C # Generic,
C # Generic
1. Define generic classes
You can create a generic class by including the angle brackets syntax in the class definition:
1 class MyGenericClass <T> 2 {3 // Add code 4}
T can follow any character of the C # naming rule. T can be regarded as the type of member variables, the type of returned attributes or methods, and the type of parameters of methods. For example:
1 class MyGenericClass <T> 2 {3 private T obj; 4 public MyGenericClass (T item) 5 {6 obj = item; 7} 8 public T Obj 9 {10 get {return obj;} 11 12} 13}
Note: 1) It cannot be assumed what type is provided for the class;
2) When comparing the type values provided by the generic type and null, you can only use = and! =.
2. default keyword
1 public MyGenericClass () 2 {3 obj = default (T); 4}
The result is that if obj is of the reference type, null is assigned to it. If obj is of the value type, the default value is assigned.