Http://www.microsoft.com/china/msdn/library/langtool/vcsharp/vbconcprogramminglanguagefuturefeatures.mspx
1. Generic Type Generic
Define a MyList <T> and set the method for this MyList class. In the method, T is used instead of the specific type float or int. The difference between types is ignored.
MyList <MyClass> list1 = new MyList <MyClass> ();
MyList <float> list2 = new MyList <float> ();
MyList <SomeStruct> list3 = new MyList <SomeStruct> ();
It can be used in Class, Struct, and Interface
In the template definition, the specific definition T belongs to the type,
Where T: struct T is value type
Where T: class T is reference type
Where T: new () T has a non-parameter constructor.
Where T: <base class name> T must be the class set by base class name or its inherited class.
Where T: <interface name> T must be an interface set by interface name or its implementation class.
My question is, what is the significance of this Generic Type? If the type is ignored, but if there is no type setting, T is the object and only a few methods can be called. If the type is set, why don't I write it directly? Why should we go around a circle?
My answer is,
First, for a generic type such as List <>, if you do not use a generic type, you need to use an array or list to achieve the goal. If it is an array, the length is limited, it is not dynamic. This is not good. If it is a list, the type is unknown and the encoding needs to be converted in the program. Therefore, the generic type is used, right?
Second, we still ensure the neglect of one type and the unification of algorithms.
2. Interator Iteration
Interator is a method that allows foreach to operate on the class. The Iterator Code defines the return type when foreach cyclically traverses elements in the set.