עליך להתחבר בכדי להוריד קבצים מהאתר
על מנת להבטיח שמשאבינו לא ינוצלו לרעה.
Generics |
1 : class Program
2 : {
3 : static void Swap<T>(ref T t1, ref T t2)
4 : {
5 : T tmp = t1;
6 : t1 = t2;
7 : t2 = tmp;
8 : }
9 : static void Main(string[] args)
10 : {
11 : int num1 = 11;
12 : int num2 = 22;
13 : Console.WriteLine("num1={0}, num2={1}", num1, num2);
14 : Swap(ref num1, ref num2);
15 : Console.WriteLine("num1={0}, num2={1}", num1, num2);
16 :
17 : string str1 = "Elchanan";
18 : string str2 = "Sonya";
19 : Console.WriteLine("str1={0}, str2={1}", str1, str2);
20 : Swap(ref str1, ref str2);
21 : Console.WriteLine("str1={0}, str2={1}", str1, str2);
22 : }
23 : }
1 : class Program
2 : {
3 : static void Main(string[] args)
4 : {
5 : List<string> names_arr = new List<string> { "Shoshana", "Yerachmiel", "Zelda", "Pirchiya", "Zalman", "Fruma" };
6 : foreach (string str in names_arr)
7 : {
8 : Console.Write(str + " ");
9 : }
10 : Console.WriteLine();
11 :
12 : List<int> nums_arr = new List<int>();
13 :
14 : Random rnd = new Random();
15 : for (int i = 0; i < 10; i++)
16 : {
17 : nums_arr.Add(rnd.Next(1000));
18 : }
19 : foreach (int num in nums_arr)
20 : {
21 : Console.Write(num + " ");
22 : }
23 : Console.WriteLine();
24 : }
25 : }
1 : class Person
2 : {
3 : private string m_FirstName;
4 : private string m_LastName;
5 : private byte m_Age;
6 : private int m_ID;
7 :
8 : public Person(string first, string last, byte age, int id)
9 : {
10 : this.m_FirstName = first;
11 : this.m_LastName = last;
12 : this.m_Age = age;
13 : this.m_ID = id;
14 : }
15 : public override string ToString()
16 : {
17 : return m_LastName + "," + m_FirstName + "," + m_Age;
18 : }
19 : }
1 : class MyGenericArray<T>
2 : {
3 : private T[] m_Arr;
4 : private int m_Counter;
5 :
6 : public MyGenericArray()
7 : {
8 : m_Arr = new T[16];
9 : }
10 : public void Add(T t)
11 : {
12 : if (m_Counter >= m_Arr.Length)
13 : Array.Resize(ref m_Arr, m_Arr.Length + 16);
14 : m_Arr[m_Counter++] = t;
15 : }
16 : public T this[int index]
17 : {
18 : get
19 : {
20 : return m_Arr[index];
21 : }
22 : }
23 : public void Print()
24 : {
25 : for (int i = 0; i < m_Counter; i++)
26 : {
27 : Console.WriteLine(m_Arr[i]);
28 : }
29 :
30 : }
31 : }
1 : class Program
2 : {
3 : static void Main(string[] args)
4 : {
5 : MyGenericArray<int> arr1 = new MyGenericArray<int>();
6 : arr1.Add(100);
7 : arr1.Add(200);
8 : arr1.Add(300);
9 : arr1.Add(400);
10 : arr1.Add(500);
11 : arr1.Print();
12 :
13 : MyGenericArray<Person> arr2 = new MyGenericArray<Person>();
14 : arr2.Add(new Person("Elimelech", "Zorkin", 65, 123456));
15 : arr2.Add(new Person("Zelda", "Gurnisht", 44, 4545454));
16 : arr2.Add(new Person("Zrubavel", "Berkovitch", 32, 8854646));
17 : arr2.Add(new Person("Vladimir", "Swisa", 12, 3434843));
18 : arr2.Print();
19 : }
20 : }
1 : class MyGenericList< T > : List< T >
2 : {
3 : public void Print()
4 : {
5 : for (int i = 0; i < base.Count; i++)
6 : {
7 : Console.WriteLine(base[i]);
8 : }
9 :
10 : }
11 : }
1 : class Program
2 : {
3 : static void Main(string[] args)
4 : {
5 : MyGenericList<string> list = new MyGenericList<string>();
6 : list.Add("Shoshana");
7 : list.Add("Yerachmiel");
8 : list.Add("Zelda");
9 : list.Add("Pirchiya");
10 : list.Add("Zalman");
11 : list.Add("Fruma");
12 :
13 : list.Print();
14 : Console.WriteLine();
15 : }
16 : }
1 : interface IMyInterface<T>
2 : {
3 : void Print();
4 :
5 : }
1 : class Person : IMyInterface<Person>
2 : {
3 : private string m_FirstName;
4 : private string m_LastName;
5 : private byte m_Age;
6 : public Person(string first, string last, byte age)
7 : {
8 : this.m_FirstName = first;
9 : this.m_LastName = last;
10 : this.m_Age = age;
11 : }
12 : public void Print()
13 : {
14 : Console.WriteLine(m_LastName + "," + m_FirstName + "," + m_Age);
15 : }
16 : }
1 : class MyGenericArray< T >
2 : {
3 : private T[] list;
4 : private int counter;
5 :
6 : public MyGenericArray()
7 : {
8 : list = new T[16];
9 : }
10 : public void Add(T t)
11 : {
12 : if (counter >= list.Length)
13 : Array.Resize< T >(ref list, list.Length + 16);
14 : list[counter++] = t;
15 : }
16 : public T this[int index]
17 : {
18 : get
19 : {
20 : return list[index];
21 : }
22 : }
23 : public void Print()
24 : {
25 : for (int i = 0; i < counter; i++)
26 : {
27 : Console.WriteLine(list[i]);
28 : }
29 : }
30 : public void PrintAll()
31 : {
32 : for (int i = 0; i < counter; i++)
33 : {
34 : (list[i] as IMyInterface< T >).Print();
35 : }
36 : }
37 : }
1 : class Program
2 : {
3 : static void Main(string[] args)
4 : {
5 : MyGenericArray<Person> arr1 = new MyGenericArray<Person>();
6 : arr1.Add(new Person("Elimelech", "Zorkin", 65));
7 : arr1.Add(new Person("Zelda", "Gurnisht", 44));
8 : arr1.Add(new Person("Muhamad", "Berkovitch", 32));
9 : arr1.Add(new Person("Vladimir", "Swisa", 12));
10 : arr1.PrintAll();
11 : Console.WriteLine();
12 : }
13 : }
1 : class Dog
2 : {
3 : private string name;
4 : private int age;
5 : public Dog(string name, int age)
6 : {
7 : this.name = name;
8 : this.age = age;
9 : }
10 : public void Print()
11 : {
12 : Console.WriteLine(name + "," + age);
13 : }
14 : }
1 : class Program
2 : {
3 : static void Main(string[] args)
4 : {
5 : MyGenericArray< Person > list1 = new MyGenericArray< Person >();
6 : list1.Add(new Person("Elimelech", "Zorkin", 65));
7 : list1.Add(new Person("Zelda", "Gurnisht", 44));
8 : list1.Add(new Person("Muhamad", "Berkovitch", 32));
9 : list1.Add(new Person("Vladimir", "Swisa", 12));
10 : list1.PrintAll();
11 : MyGenericArray< Dog > list2 = new MyGenericArray<Dog>();
12 : list2.Add(new Dog("Aldo", 1));
13 : list2.Add(new Dog("Edison", 6));
14 : list2.Add(new Dog("Boots", 6));
15 : list2.PrintAll();
16 : }
17 : }
where T : struct | הטיפוס חייב להיות ValueType . | |
where T : class | הטיפוס חייב להיות Reference Type. | |
where T : BaseClass | הטיפוס חייב להיות מחלקת בסיס או אחת מניגזרותיה. | |
where T : IInterface | הטיפוס חייב לממש את הממשק IInterface. | |
where T : new() | לטיפוס חייב להיות Default Ctor. |
1 : public delegate void MyDelegate< T >(T t);
2 : class Program
3 : {
4 : static void Main(string[] args)
5 : {
6 : Person p = new Person("isaschar", "shemyafe", 78);
7 : MyDelegate<Person> del1 = new MyDelegate<Person>(Program.Func);
8 : del1(p);
9 : MyDelegate<int> del2 = new MyDelegate<int>(Program.Func);
10 : del2(123);
11 : }
12 : static void Func<T>(T t)
13 : {
14 : Console.WriteLine(t);
15 : }
16 : }