C # concept and features

Source: Internet
Author: User

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 relates to the template in Iso c ++. Indeed, the syntax of the two is very similar, however, the polymorphism compilation of the two is very different from that of the existing one. after C ++'s template is compiled, the Code with template is not compiled. it is a macro replacement process. each time you use the template type, the compiler generates a corresponding type code. regardless of whether the type of code has been used, www.zxbc.cn.
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 not related to language. at last, any language is compiled into an intermediate language. In this way, the paradigm supported by IL and CLR can be applied to all CLR-based languages, 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.

During this operation, the program will create a new instance of the templatestack class, where each t is replaced by an integer parameter provided. In fact, when a program uses the integer parameter to create a new instance of the templatestack class, the project array locally stored in the templatestack class will be an integer rather than an object. The program also eliminates the packing problem associated with pushing integers into the stack. In addition, when a program pops up a project from the stack, you do not need to explicitly convert it to the corresponding type, because the current specific instance of the stack class stores integers locally in its data structure.
--------------------------------------------
Advantages of the usage model:
The programmer only needs to write, test, and deploy the code once, that is, the code can be reused for different data types.
At the same time, the compiler also checks the model. When a program uses the provided type parameter to instantiate a model class, this type parameter can only be the type specified by the program in the class definition. For example, if the program creates a stack of the customer object type, it cannot push integers into the stack. By performing this operation forcibly, you can generate more reliable code.
In addition, compared with other strict type implementations, the generic C # implementation reduces the code expansion speed. Creating a set of types with generics can avoid creating specific variants of each class while maintaining operational performance advantages. For example, a program can create a parameterized Stack class without the need to create integerstack for storing integers, stringstack for storing strings, and mermerstack for storing customer types.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.