Abstract: This article describes how to define a C # generic class and how to implement the inheritance, methods, and constraints of the generic class. C # generics parameterize types and abstract them as parameters, so that we can better reuse code in practical use, at the same time, it provides stronger type security and higher efficiency, but in terms of constraints, it only supports display constraints, so it is not so flexible. I think it can provide higher efficiency because generics adopt the "on-demand" mode during instantiation, that is, on-demand instantiation occurs In JIT (Just In Time) compile time. Next let's take a look at how to define a C # generic class. It's very simple. You just need to realize that here, the type has been parameterized: copy the code using System; using System. collections. generic; using System. text; namespace GenericTest {class Program {static void Main (string [] args) {// use string, int to instantiate Test <T, S> class Test <string, int> t = new Test <string, int> ("SHY520", 22); // call the method t in the generic class. setValue () ;}/ ** // <summary> // defines a generic class, which has two type parameters, T, S // http://www.cnblogs.com/jara /// </summ Ary> // <typeparam name = "T"> type parameter </typeparam> // <typeparam name = "S"> type parameter </typeparam> public class Test <t, s> {// The type parameter of the generic class can be used for the class member private T name; private S age; public Test (T Name, S Age) {this. name = Name; this. age = Age;} public void SetValue () {Console. writeLine (name. toString (); Console. writeLine (age. toString () ;}} the above example of copying the code is not very appropriate, so that you can understand the definition and instantiation method of generics for beginners, as shown above, we have defined a generic class, so how to implement C # generic class inheritance What about it? Here, we need to satisfy any of the following two points: 1. In generic class inheritance, the type parameter of the parent class has been instantiated. In this case, the subclass is not necessarily a generic class; 2. the type parameter of the parent class is not instantiated, but comes from the subclass. That is to say, both the parent class and the subclass are generic classes, and both have the same type parameter; copy the code // if you write it like this, it is clear that the type T cannot be found. The S error public class TestChild: Test <T, S >{} // the correct format should be public class TestChild: Test <string, int >{} public class TestChild <T, S>: Test <T, S >{} public class TestChild <T, S >:test <String, int >{} copy the code and then let's take a look at the generic interface, its creation and inheritance rules are the same as the generic classes mentioned above. See the following code: copy the code public Interface IList <T> {T [] GetElements ();} public interface IDictionary <K, V> {void Add (K key, V value );} // The type parameters of the generic interface are either instantiated // or the class List parameter <T>: IList <T>, IDictionary <int, t> {public T [] GetElements () {return null;} public void Add (int index, T value) {}} copy the code to see C # generic delegation, first, we define a delegate whose type parameter is T, and then use the delegate call method in the class: copy the code using System; using System. collections. generic; using System. text; namespace GenericTest {// define a delegate. The type parameter is T, return value Type T // generic delegation supports applying the type parameter delegate string GenericDelete <T> (T value) on return values and parameters; class test {static string F (int I) {return "SHY520";} static string G (string s) {return "SHY520";} static void Main (string [] args) {GenericDelete <string> G1 = G; genericDelete <int> G2 = new GenericDelete <int> (F) ;}} copy the code. Let's take a look at the C # Generic method, the generic mechanism of c # only supports containing type parameters in method declarations, that is, generic methods. Note that Generics do not support the use of type parameters on other class/interface members except methods, but these Members can be included in the generic type, you can also use generic type parameters. Another point to note is that generic methods can exist in generic types or non-generic types. Next, let's take a look at the Declaration, call, overload, and overwrite of generic types. Copy the code using System; using System. collections. generic; using System. text; namespace GenericTest {class GenericClass {// declare a generic method public T getvalue <T> (T t) {return t ;}// call a generic method // note: when calling a generic method, instantiate the type parameter public int useMethod () {return this. getvalue <int> (10) ;}// reload the getvalue method public int getvalue (int I) {return I ;}// the following example overwrites the getvalue <int> (10, when a generic method is overwritten, the constraint is inherited by default. You do not need to re-specify the Constraint Relation abstract class Parent {public abst Ract k test <K, V> (K k, V v) where K: V;} class Child: Parent {public override t test <T, S> (T t T, S s) {return t ;}} copy the code. Finally, let's take a look at the constraints in C # generics: generics in C # only support the display constraints, in this way, the type security required by C # can be ensured, but the displayed constraints are not required. Without the constraints, generic type parameters can only access System. public methods in Object type. Explicit Constraints are expressed by the where clause. You can specify "base class constraints", "interface constraints", and "constructor constraints ", there are four types of constraints: Value Type and reference type constraint. The following example is from the lecture PPT of instructor Li Jianzhong. 1. base class constraints: copy the Code class A {public void F1 () {}} class B {public void F2 () {}} class C <S, T> where S: a/S inherits from A where T: B // T inherits from B {// you can call F1 on A variable of type S, // you can call F2 on a variable of type T to copy code. 2. interface constraint copy code interface IPrintable {void Print ();} interface IComparable <T> {int CompareTo (T v);} interface IKeyProvider <T> {T GetKey ();} class Dictionary <K, V> where K: IComparable <K> where V: IPrintable, IKeyProvider <K> {// you can call CompareTo on a variable of K type, // you can call Print and GetKey} on A variable of the Type V to copy the code. 3. The constructor constraints copy the Code class A {public () {}} class B {public B (int I) {}} class C <T> where T: new () {// you can use T t = new T ();} C <A> c = new C <A> (); // yes, A has no parameter constructor C <B> c = new C <B> (); // error, B does not have A parameter-free constructor to copy Code 4. Value/reference type constraints copy code public struct A {} public class B {} class C <T> where T: struct {// T here is A value type} C <A> c = new C <A> (); // yes, A is A value type C <B> c = new C <B> (); // error. B is A reference type.