In my understanding, generics are like a bag that can hold everything. we certainly don't want to use this bag to pack delicious food and garbage. for example, if you go to the supermarket to buy a package of cookies, the waiter will give you a bag of cookies when you check out. but for us, both the biscuit and biscuit bags are packed in this big bag, And the biscuit is what we need, and the packaging bag is rubbish. this introduces the concept of "packing and unpacking ". the process of packing the bagged cookies is called "Packing", and the process of packing and eating the cookies is called "unpacking ". obviously, this is a waste of time for eating cookies. therefore, we may need a bag that can be used for self-prompting. That is to say, if you want to use this bag to pack cookies, then you will be prompted automatically when you use it to pack candy, it has been specified as a cookie.
Although the above example is not very relevant, it is helpful for understanding generics.
There are several Collection types:
1. List, which is the most generic type of our applications. It corresponds to the arraylist set.
2. dictionary. This is also a set of commonly used generic types that correspond to hashtable.
3. Collection corresponds to collectionbase
4. readonlycollection corresponds to readonlycollectionbase, which is a read-only set.
5. queue, stack, and sortedlist correspond to non-generic classes with the same name as them.
Take a look at this class:
Personcollection
1 Public Class Personcollection: ilist
2 {
3 Private Arraylist _ persons = New Arraylist ();
4 Public Person This [ Int Index]
5 {Get {Return(Person) _ persons [Index];}}
6 Public Int Add (person item)
7 {
8_ Persons. Add (item );
9Return_ Persons. Count- 1;
10}
11 Public Void Remove (person item)
12 {_ Persons. Remove (item );}
13
14 Object Ilist. This [ Int Index]
15 {
16 Get {Return_ Persons [Index];}
17 Set {_ Persons [Index]=(Person) value ;}
18 }
19 Int Ilist. Add ( Object Item)
20 {ReturnAdd (person) item );}
21 Void Ilist. Remove ( Object Item)
22 {Remove (person) item );}
23
24
25 }
26
27
This class is an operation class of the person class. You can freely add or delete the person class. if you want to write an operation class with the same function as other classes, I believe you only need to replace the person class. however, after learning about generics, you can use them like this.
List <person> Persons = new list <person> ();
Persons. Add (new person ());
Person = persons [0];
For example, to replace the person class with the employee class, you only need to write it like this.
List <employee> employees = new list <employee> ();
Employees. Add (new employee ());
Employee Employee = employees [0];
List is a generic class defined in C #. Now we can define it ourselves.
Typehelper
1 Public Class Typehelper < T >
2
3 {
4
5 Public String GetType (T) {
6
7Return "Type is"+T. GetType (). tostring ();
8}
9 }
10
11
Here T is just a type placeholder. in actual application, replace T with the actual type.
Typehelper <person> typehelper = new typehelper ();
Typehelper. GetType (person );
Note that t is only a placeholder. In fact, it can be replaced with any symbol. Do not use t as a placeholder.
Sometimes we have to constrain the actual types, such as the following generic classes.
Collectionhelper
1 Public Class Collectionhelper < T, V >
2
3 {
4
5 Private T collec = New T ();
6
7 Public Int Indexof (V) {
8
9ReturnCollec. indexof (v );
10
11}
12 }
13
T and V in a generic class can be replaced by any type. First, T must have an indexof method and V must be a reference type. Therefore, you need to modify this class.
Collectionhelper
1 Public Class Collectionhelper < T, V > Where T: ilist
2
3 Where V: Class
4
5 {
6
7 Private T collec = New List ();
8
9 Public Int Indexof (V) {
10
11ReturnCollec. indexof (v );
12
13}
14
15 }
16
Where is the keyword, and T is the type of the constraint to be bound. ilist indicates the interface to be implemented. It indicates that as long as the ilist interface is implemented, there must be an indexof method. The constraint of V must be a class.
If the required type must be a value type parameter, you need to use struct. if you need to re-instantiate the type, you need to use new () to restrict it. It means that this type must have a constructor without parameters.
Note that if a type requires multiple constraints, new () must be written at the end.