1 overview
1.1 Introducing a generic method
In some cases, there may be only a few method members in a type that use the type parameter, and it is not necessarily necessary to define the entire type as generic. For example, in the following code, the generic class gc<t> defines a static method contain that determines whether an element exists in an array:
Public classGc<t>{ //static fields Static ReadOnly DoublePi=3.14; //Method Public Static BOOLcontain (t[] ts,t tp) {foreach(T t1inchts) { if(t1. Equals (TP))return true; } return false; }}
Each time the method is called, you need to specify an enclosing construction type for the generic class:
Short[] Sarray =New Short[] {1,3, the,255};BOOLB1 = gc< Short. Contain (Sarray, Short. MaxValue);//falseint[] IArray =New int[]{1,3, the,255,65535};BOOLb2=gc<int. Contain (IArray,ushort. MaxValue);//true
A static field pi is defined in the generic class gc<t>. Because static members are owned by the constructed type of a generic class, the field is allocated storage space for each constructed type.
The following program transfers the type parameter from the definition of the class to the definition of the method, which is the generic method:
classGenericsmethodsample {Static voidMain () { Short[] Sarray =New Short[] {1,3, the,255 }; Console.WriteLine (C.contain< Short> (Sarray, Short. MaxValue));//false int[] IArray =New int[] {1,3, the,255,65535 }; Console.WriteLine (C.contain<int> (IArray,ushort. MaxValue));//true } } /// <summary> ///generic Method Class/// </summary> Public classC {//static fields Static ReadOnly DoublePI =3.14; //Generic Methods Public Static BOOLContain<t>(t[] ts, T tp) {foreach(T t1inchts) { if(t1. Equals (TP))return true; } return false; } }
In the above program, the two-time method calls are made by the same class, and the static field pi only consumes one store in memory.
1.2 Definition of generic methods
Defining a generic method also includes the type parameter in a pair of delimiter <> after the method name. If there are more than one type parameter, the "," number is split between each other. After that, the type parameter defined can be either the parameter type and the return type of the method, or it can be used to define a local variable in the execution code of the method. In addition, the definition rules for generic methods are the same as those for normal methods.
If more than one generic method is defined in the same type, their type parameters are unrelated.
If a generic method belongs to a generic type, and both define the same type parameter, the use of the type parameter is also subject to the "masking rule", that is, the type parameter that appears in the generic method is defined by the method, and the type parameter that appears in the other members of the type belongs to the definition of the class. For example:
Public class Garith<t>{ private t[] m_list; Public Static void Swap<t> (ref T TP1,ref t TP2) { ... }}
At this point, any instance of a constructed type is created for the generic class garith<t>, and its private field M_list type is replaced with the corresponding enclosing type, but does not affect the parameter type of the generic method swap<t>. Similarly, the enclosing type that is assigned to the method is independent of the class's constructed type. For example:
int x=2; int y=5; Garith<string;. swap<int> (ref X,ref y); // correct garith<int;. swap<string> (ref X,ref y); // error: Delivery type differs from actual type
Therefore, to improve the readability of the program, you should try to avoid defining a type parameter with the same name for the generic type and its generic member methods.
In a generic method, you can also restrict type parameters to the same way as the generic class
Public Static where T:icomparable<t>{ if0) return TP1; Else return TP2;}
A generic method can belong to either a normal type or a generic type (generic class, generic struct, generic interface).
Defining properties, events, index functions, and operators for generics is not allowed in C #.
11th Chapter Generic method