Generic is. A new feature introduced in. NET 2.0, from. it has been several years since the release of NET 2.0, nowadays, many companies like to ask questions such as whether generic is used, what is generic, and how to write generic data during interviews. It seems that generics are uncertain, just as if they were used by common people. In fact, although each. NET programmers have different work content, but. the use of some basic things in NET is not very different. I don't want to use generics for students who have just graduated from C. Below I will briefly talk about generics.
Generics introduce the concept of type parameters into. NET. type parameters enable classes and methods to postpone the designation of one or more types until the client Code declares and instantiates the class or method. With generics, You can reuse code to the maximum extent, protect type security, and improve performance.
Generic parameters
In the definition of generic classes or methods, type parameters are placeholders of specific types specified by client code when instantiating generic variables. We usually use T as a placeholder for type parameters, but this is not necessary. We can use some more meaningful descriptive names as type placeholders, such as TInput and TOutput.
public class List<TInput, TOutput>
Type parameter Constraints
When defining generic types, you can restrict the types used for type parameters when the client code instantiates a class. If the client uses a type that violates the constraints to instantiate the type, a compilation error occurs. The constraint is specified using the where keyword.
| End |
Description |
| Where T: struct |
The type parameter must be a value type. |
| Where T: class |
The type parameter must be of the reference type. |
| Where T: new () |
The type parameter must have a public and non-parameter constructor. |
| Where T: <base classname> |
The type parameter must inherit from the specified base class) |
| Where T: <interface name> |
The type parameter must be the specified interface or the type that implements the specified Interface |
| Where T: U |
The type parameter provided for T must be a parameter provided for U or derived from a parameter provided for U. |
public class Student { public Student(string name, int age) { Name = name; Age = age; } public string Name { get; set; } public int Age { get; set; } } public class Teacher<T> where T : new() { public string Name { get; set; } public int Age { get; set; } public string Course { get; set; } }
The code above defines a Student class and a Teacher class. The Student class has only one constructor with two parameters. The Teacher class is a generic class. Its type parameters must have a constructor without parameters. If you use the Student class to instantiate the Teacher class editor, an error message is displayed.
Using constraints makes it safer to perform operations on generic members.
Generic Type
Generic classes encapsulate operations that are not specific to specific data types. Generic classes are usually used for collections. Operations such as adding or removing items from a set are roughly the same and have nothing to do with the data type. You can add multiple constraints to a generic class. For example, we will limit the generic type parameters of the above-defined Teacher <T> generic class to the reference type. Note that when multiple constraints are used, if new () constraints exist, new () must be placed at the end.
public class Teacher<T> where T : class, new() { public string Name { get; set; } public int Age { get; set; } public string Course { get; set; } }
Generic Interface
It is usually useful to specify an interface for a generic class of a generic set or set item. When you specify an interface as a generic type parameter, you can only use the type that implements this interface. For example, the following section adds the IComparable <T> interface Restriction Conditions for the type parameters of the Teacher <T> generic class, in this way, only the reference type of the constructor that implements the interface IComparable <T> and has no parameter can be used as the type parameter when the Teacher <T> is instantiated.
public class Teacher<T> where T : class, IComparable<T>, new() { public string Name { get; set; } public int Age { get; set; } public string Course { get; set; } }
Generic Method
The generic method is to use the parameter declaration method of the generic type. Example:
static void Swap<T>(ref T lhs, ref T rhs){ T temp; temp = lhs; lhs = rhs; rhs = temp;}
The following describes how to call a generic method:
int a = 1; int b = 2; Swap<int>(ref a, ref b);
You can also omit the generic type parameter when calling a generic method. The compiler will deduce its type based on the type of the input parameter. The preceding call method can also be written as follows:
Swap(ref a, ref b);
Generic methods can also be overloaded by generic type parameters.
void DoWork<T>() { }void DoWork<T, U>() { }
Generic Delegation
A delegate can also define a type parameter. You can call a generic delegate just like calling a generic method in a generic class. For example, the generic delegate Action without return value defined in the System namespace <T>.
public delegate void Action<in T>(T obj)
For example, the ForEach (Action <T> action) parameter of List <T> is a generic delegate, you can input an Action <T> generic delegate instance to perform the same operation on each element in the list.
[TestMethod] public void TestMethod1() { List<String> names = new List<String>(); names.Add("Bruce"); names.Add("Alfred"); names.Add("Tim"); names.Add("Richard"); names.ForEach(Print); } private void Print(string s) { Console.WriteLine(s); }
When using a generic type parameter, we cannot set the default value because we do not know whether the parameter is a value type or a reference type. The solution is to use the default keyword. The default keyword returns null for the reference type, 0 for the value type, and a structure member initialized to 0 or null for the structure.
Reference: http://msdn.microsoft.com/en-