What is new in c sharp 2.0 -- study from msdn

Source: Internet
Author: User

Http://www.microsoft.com/china/msdn/library/langtool/vcsharp/vbconcprogramminglanguagefuturefeatures.mspx
1. Generic Type Generic
Define a MyList <T> and set the method for this MyList class. In the method, T is used instead of the specific type float or int. The difference between types is ignored.
MyList <MyClass> list1 = new MyList <MyClass> ();
MyList <float> list2 = new MyList <float> ();
MyList <SomeStruct> list3 = new MyList <SomeStruct> ();
It can be used in Class, Struct, and Interface
In the template definition, the specific definition T belongs to the type,
Where T: struct T is value type
Where T: class T is reference type
Where T: new () T has a non-parameter constructor.
Where T: <base class name> T must be the class set by base class name or its inherited class.
Where T: <interface name> T must be an interface set by interface name or its implementation class.
My question is, what is the significance of this Generic Type? If the type is ignored, but if there is no type setting, T is the object and only a few methods can be called. If the type is set, why don't I write it directly? Why should we go around a circle?
My answer is,
First, for a generic type such as List <>, if you do not use a generic type, you need to use an array or list to achieve the goal. If it is an array, the length is limited, it is not dynamic. This is not good. If it is a list, the type is unknown and the encoding needs to be converted in the program. Therefore, the generic type is used, right?
Second, we still ensure the neglect of one type and the unification of algorithms.
2. Interator Iteration
Interator is a method that allows foreach to operate on the class. The Iterator Code defines the return type when foreach cyclically traverses elements in the set.

Related Article

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.