Analyze the new features of C #2.0-generics)
Author: Liang Zhen [MS-MVP]
Model is to improve object-oriented Program Derived from polymorphism design.
1. C # Design Review and Prospect of Polymorphism
Before introducing the concept of paradigm, let's review the definition of object types in 1.0 or 1.1:
The object type is an alias of system. Object in. NET Framework. It can be assigned to any type of variables of the object type.
With the introduction of object types, the. NET Design for object-oriented program polymorphism is realized.
Because the object itself is a reference type, it is stored on heap (HEAP. It is easy to convert other reference types and objects.
The value type and object type conversion require two concepts for object type and value type conversion: boxing and unboxing.
(1) boxing package installation
Converts a value type to the object type,
Example: int I = 1; // stored on the stack
Object o = (object) I; // stored on the stack
The above Code In Il, it will be box [mscorlib] system. int32
Load int32 and 1 into an object at the same time. The structure is as follows:
Object [{int32}-{1}]; // The former indicates the boxing type, and the latter indicates its value.
(2) unboxing
Convert a value type that has been packaged into the obect type back to the value type
The operation is divided into two parts:
A. First, check whether the converted back type is the package type. If it is not to throw an invalidcastexception runtime error.
B. Copy the value of the object type to the variable of the target value type;
For example:
Int I = 1;
Object o = (object) I; // boxing
Int J = (INT) O // unboxing
// Double D = (double) O, with a running error
From the above, we can see that in. NET Framework 1.0, the object Object Design polymorphism ratio is C ++'s template (a macro-like Encoding
Translation replacement) execution efficiency increases the overhead of copy. Therefore, the pattern is introduced in. Net framewrok 2.0 to improve high object-oriented program polymorphism.
Design.
2. Concept and features of the model:
The design of the model is to solve two problems in the above mentioned object polymorphism design:
(1) In terms of performance, boxing and unboxing require a large amount of replication overhead;
(2) security. In the preceding example, we can see that if the unboxing type is different, an invalidcastexception exception will be thrown;
The design format of the fan type is to use <and> to close one of the fan parameters, for example:
Public class Stack <t>;
The instantiation format of the fan type is to use the type to replace <and> to close one of the fan type parameters, for example:
Stack <char> char_stack = new stack <char> ();
Defines the format of a Multi-fan type. Multiple fan type parameters are closed in <and>. For example:
Class node <K, T>
For C ++ programmers, the preceding format of the model quickly contacts the template in Iso c ++;
Indeed, the two syntaxes are very similar, but their polymorphism compilation is quite different from the existing ones.
After the C ++ template is compiled, the Code with a template is not compiled, but is replaced by a macro.
Each time you use the template type, the compiler generates a corresponding type code, regardless of whether the type code has been used.
In C #2.0, the paradigm is supported in the intermediate language (IL) and the Common Language Runtime (CLR.
For value type: the parameter type will be replaced during JIT compilation. If a specific type of machine code exists and compiled, this code will be directly returned.
This avoids code expansion caused by template in Iso c ++.
For reference type: the parameter type will be replaced directly during JIT compilation.
To understand C #2.0, it is very important to implement CLR-based support, because. NET is essentially language-independent.
Inter-language. In this way, all CLR-based languages can be applied, such as Visual Basic 2005.
3. Examples of comparison between the execution efficiency of the model and other types
The following are the classes of the three stacks constructed using the int, object, and fan types respectively.
/// <Summary>
/// Stack implemented by INT type
/// </Summary>
Class intstack
{
Private int [] data;
Private int current;
Private int length;
Public intstack (INT length)
{
Length = length;
Current = 0;
Data = new int [length];
}
Public int top ()
{
Return data [Current-1];
}
Public void push (INT data)
{
If (current <length)
{
Data [current ++] = data;
}
}
Public void POP ()
{
If (current> 0)
{
Current --;
}
}
}
/// <Summary>
/// Standard Stack
/// </Summary>
/// <Typeparam name = "T"> Model </typeparam>
Class templatestack <t>
{
Private int length;
Private int current;
Private T [] data;
Public templatestack (INT length)
{
Current = 0;
Length = length;
Data = new T [length];
}
Public t top ()
{
Return data [Current-1];
}
Public void push (t data)
{
If (current <length)
{
Data [current ++] = data;
}
}
Public void POP ()
{
If (current> 0)
{
Current --;
}
}
}
/// <Summary>
/// Object Stack
/// </Summary>
Class objectstack
{
Private object [] data;
Private int current;
Private int length;
Public objectstack (INT length)
{
Length = length;
Current = 0;
Data = new object [length];
}
Public object top ()
{
Return data [Current-1];
}
Public void push (Object Data)
{
If (current <length)
{
Data [current ++] = data;
}
}
Public void POP ()
{
If (current> 0)
{
Current --;
}
}
}
The overhead of constructing an int stack is close to that of using the int stack and the fan stack.
Compared to the previous two objects, the unboxig overhead is doubled for each increase.
4. Appendix. NET 2.0 Framework fan-type container list:
Comparer <t> comparer comparison
Dictionary <K, T> hashtable hash table
Linked List <t> linklist linked list
List <t> arraylist array linked list
Queue <t> queue
Sorteddictionary <K, T> sortedlist
Stack <t> Stack
Icollection <t> icollection container Interface
Icomparable <t> system. icomparable comparison Interface
Idictionary <K, T> idictionary dictionary Interface
Ienumerable <t> ienumerable enumeration Interface
Ienumerator <t> ienumerator downloading interface
Ilist <t> ilist linked list Interface
References:
Design and Implementation of generics for the. NET Common Language Runtime
--- Andrew Kennedy don Syme (Microsoft Research, Cambridge, U. K .)
An Introduction to C # generics
--- Juval Lowy idesign (msdn online)
C # programmer's reference
--- Msdn Library (msdn online)