23.1 Concepts of generics
(1) In a common set, the elements are considered to be object types, with the following drawbacks
(a) When assigning a value to the type of object, the use of the object to the corresponding type, in boxing and unboxing caused a certain performance loss;
(b) Any type of data can be placed in the set, not conducive to type safety inspection;
Static voidMain (string[] args) {Queue Q=NewQueue (); Q.enqueue (Ten); Q.enqueue ("wo");//Q.enqueue (1); foreach(intIteminchQ//string cannot be converted to int type output{Console.WriteLine (item); Console.read (); }
(2) Generic collection: generic sets and normal collection functions are the same, the only difference being that the type of the generic collection element is not the type of object, but rather the type we make
Define:queue<t> myque =new queue<t> (); (T is the type we can specify)
// (2) Generic collection: generic sets and normal collection functions are the same, the only difference being that the type of the generic collection element is not the type of object, but rather the type we make // define:queue<t> myque =new queue<intnew queue<int>(); My. Enqueue (ten); // //error type errors do not match, cannot assign my. Enqueue (1);
(3) In addition to having a generic set, you can also define generic functions, structs, delegates, and so on, simply add <T> after the function name or collection name
classProgram {Static voidMain (string[] args) { } //(3) In addition to having a generic set, you can also define generic functions, structs, delegates, and so on, simply add <T> after the function name or collection name Public Static voidSwap<t> (refT A,refT B) {T C; C=A; A=b; b=A; } } //(4) Multiple type parameters can also be included in the definition of a generic type Public structMystruce<v,k> { }
23.2 Generic collection Classes
C # definition and many common generic collection classes, in System.Collections.Generic space
(1) The List<t> class corresponds to a non-generic set ArrayList
//(1) The List<t> class corresponds to a non-generic set ArrayListlist<string> Basketballplayer =Newlist<string> (5); Basketballplayer. ADD ("yaoming"); Basketballplayer. ADD ("Kobi"); Basketballplayer. ADD ("Maidi"); foreach(stringIteminchBasketballplayer) {Console.WriteLine (item); } console.read ();
(2) Stack<t> class corresponding to non-generic set Stack
//(2) stack<t> class corresponding to non-generic set Stackstack<int> s=Newstack<int>(); S.push (1); S.push (2); S.push (3); S.push (4); foreach(varIteminchs) {Console.WriteLine (item); } console.read (); Console.WriteLine ("************");
(3) Queue<t> class corresponding to non-generic set Queue
// (3) Queue<t> class corresponding to non-generic set Queue Queue<intnew queue<int>(); Q.enqueue (1); Q.enqueue (2); Q.enqueue (3); foreach (var in q) { Console.WriteLine (item); } Console.read ();
23.3 type constraints for generics
(1) in generics, because generic type T can be any type, in some cases (due to the inability to determine the type of T) there is a problem, as follows:
(2) Here, because item is of type T, the system cannot determine its type, so it is not possible to determine whether the type contains a name member and therefore an error
At this point, use the type constraint (the keyword where T: + restricted range) where t:aniamal can limit T to Aniamal and its subclasses, so you know the T type, so there's no error.
classProgram {Static voidMain (string[] args) { } } //23.3 type constraints for generics//(1) in generics, because generic type T can be any type, in some cases (due to the inability to determine the type of T) there is a problem, as follows: classAnimalfamily<t>//class animalfamily<t> where T:aniamal { PublicList<t> t=NewList<t>(); Public voidaddlist (T Item) {T.add (item); } Public voidDisplay () {foreach(varItemincht) {Console.WriteLine (item.name); //error 1 "T" does not contain the definition of "name" and cannot find an extension method "name" that accepts the first parameter of type "T" (Is it missing a using directive or an assembly reference?) E:\ Open can \c#\code\ 23rd Chapter \test04\program.cs Test04//(2) Here, because item is of type T, the system cannot determine its type, so it is not possible to determine whether the type contains a name member and therefore an error//At this point, use the type constraint (the keyword where T: + restricted range) where t:aniamal can limit T to Aniamal and its subclasses, so you know the T type, so there's no error. } } } classAniamal { Public stringname; }
The note of C # 's door--23rd chapter