Program.cs 1.63 KB
Newer Older
Uwe Wienkop's avatar
Uwe Wienkop committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;

namespace _01Indexer
{
    class Person
    {
        public string Name { get; private set; }
        public Person(string Name) { this.Name = Name; }
    }
    class PersonenManager
    {
        private Person[] personen = new Person[10];
        public Person this[int x]
        {
            get => personen[x];
            set { personen[x] = value; }
        }
        #region Weitere Möglichkeiten
        public Person[] this[string text]
        {
            get
            {
                int anz = 0;
                foreach (Person p in personen)
                {
                    if (p != null && p.Name.Contains(text))
                        anz++;                    
                }
                if (anz == 0)
                    return null;
                Person[] pfeld = new Person[anz];
                anz = 0;
                foreach (Person p in personen)
                {
                    if (p != null && p.Name.Contains(text))
                        pfeld[anz++] = p;
                }
                return pfeld;
            }
        }
        #endregion
    }
    class Program
    {
        static void Main(string[] args)
        {
            PersonenManager pm1 = new PersonenManager();
            pm1[0] = new Person("Anton");
            pm1[1] = new Person("Berta");
            pm1[2] = new Person("Claudia");
            pm1[3] = new Person("Dieter");

            #region Weitere Möglichkeiten
            Person[] pers = pm1["er"];
            foreach (Person p in pers)
            {
                Console.WriteLine(p.Name);
            }
            #endregion
        }
    }
}