And the role of generic conventions
Improve performance
拆箱和装箱
From the value type into a reference type装箱
, the reference type for the converted value type拆箱
Packing and unpacking is easy to use, but the performance loss is relatively large, especially when traversing many items.
List<T>
The object is not used, the definition of the type in use
var list = new List<int>();
list.Add(44); // no boxing
int item = list[0]; // mo unboxing
It might be
List<T>
seen as a new type, not specifically the C ++ templates and comparing;
Generic type of naming convention
- Generic type with the name
T
prefix; Generic type allows the use of any type of substitutional and using only one generic type can be used
T
as the name of the generic typepublic class List<T>{} public class LinkedList<T>{}
If the generic type specific requirements (e.g. or must implement an interface that is derived from the base class), or the use of
两个或多个泛型类型
public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e); public delegate TOutput Converter<TInput, TOutput>(TInput from); public class SOrtedList<TKey, TValue>{}
Function # generic class of
## defaults
When you create a generic, not given the null generic type ; at this time, we need
default
thenull
given reference type, value 0 is assigned to type;
public T GetDocument()
{
T doc = default(T);
lock(this)
{
doc = documentQueue.Dequeue();
}
return doc;
}
## Constraints
If the generic class method requires a generic type of call, you must add constraint
where
Generics supports the following types of constraints:
constraint | Explanation |
---|---|
where T: struct | For structural constraints, T must be a value type |
where T: class | T must be a reference type |
where T: Ifo | T must implement the interface IFoo |
where T: Foo | T must derive the base class Foo |
where T: new() | Constructor constraint, T must have a default constructor |
Generic types may be combined plurality of constraints, where T: IFoo, new()
constraints, and MyMerge<T>
affirms specified, T IFoo must implement the interface, and must have a default constructor, example is shown below:
public class MyMerge<T> where T: IFoo, new()
{
// dosomething
}
inherit
Generic type generic interface may be implemented, it may be derived from the classes, of course, also be derived from the generic base class;
public class Base<T>{}
public class Derived<T>: Base<T> {} // 派生自泛型基类
Of course, derived from the generic type specified type base class:
public class Base<T>{}
public class Derived<T>: Base<string>{}
Static member
Static members of a generic class can only share one instance of the class
// 定一个泛型类的静态成员
public class StaticDemo<T>
{
public static int x;
}
StaticDemo<int>.x = 3; // 第一组静态字段 = 3
StaticDemo<string>.x = 4; // 第二组静态字段 = 4
Console.WriteLine(StaticDemo<int>.x); // 这里将会输出3
# Generic interface
Use generic to define an interface, the interface definition may be in the process with the generic parameter
Covariant
In .Net, the parameter type
协变
of
For example, Shape and Circle class, Circle derive from Shape , Shape Display method is acceptable for the type of the parameter
public void Display(Shape object){}
Can now be derived from any objects passing Shape base class, e.g. Circle, Rectangle
Circle c = new Circle(5);
Display(c); // 这里便是协变
Resistance to change
Return type of the method is
抗变
the
For example, if the method returns a Shape, it will not be able to confer Circle, but in turn you can;
Covariant generic interface
If the use of generic type
out
annotation, the interface is generic协变
, meaning that the return type is T
if the use of generic typein
annotation, the interface is generic抗变
, indicating that the incoming parameter type only T