What is new in C sharp 2.0--study from MSDN

Source: Internet
Author: User
Tags foreach anonymous definition empty

Http://www.microsoft.com/china/msdn/library/langtool/vcsharp/vbconcprogramminglanguagefuturefeatures.mspx
1. Generic Type generics
Defines a mylist<t&gt, the method of setting the MyList class, using T instead of the specific type float or int, ignoring the difference between types
Mylist<myclass> List1 = new mylist<myclass> ();
mylist<float> list2 = new mylist<float> ();
mylist<somestruct> list3 = new mylist<somestruct> ();
can be used in class, struct, and interface
In the definition of a template, a specific definition of the type T belongs to,
Where T:struct T is the value type
Where T:class T is reference type
where T:new () T has a constructor with no parameters
where T: <base class name> T must be a class set by base class name or his inheriting class
where T: <interface name> T must be an interface set by interface name or its implementation class
My question is, what's the point of this generic type? If you ignore the type, but if there is no type setting, then T is object, only a few methods can be invoked, and if the type is set, then why don't I write it directly? Why do you have to go around a circle?
My answer is,
First, for list<> such generics, if not the use of generics, to achieve the purpose, it is necessary to use an array or list, if the array, then the length is limited, not dynamic, this is not good, if the list, then the type is not known, You need to encode in your program to convert, so you use generics, right?
Second, it still guarantees a type above the neglect, the unification of the algorithm

2. Interator Iteration
Interator is a method that allows foreach to operate on a class, and the iterator code defines the return type of the Foreach loop traversing the elements in the collection.
avoided if a collection to support the Foreach loop must implement System.Collections.IEnumerable or System.Collections.IEnumerator
Yield return, the returned value must be System.Collections.IEnumerable or System.Collections.IEnumerator

3. Anonymous Method Anonymous Methods
Btnok.click + = delegate{messagebox.show ("OK")}
Btnok.click + + new EventHandler (object sender, EventArgs e)
{
MessageBox.Show (sender. ToString ());
}
A little bit of code is passed as a parameter to the delegate
This makes it easy to set up running code in runtime, external script, injection ah, reject

4. Partial class Local classes
A class, which can be continuously supplemented and defined in the various CS files of a assembly or module (Exe/dll), facilitates the development of multiple developers, and Partial class is useful for a class that is written by several people. Everyone can supplement the partial class with their own needs
It should be noted that, first, it can only be in a assembly or module; second, the way of not being able to declare is not uniform

5. Nullable Types Empty type
The way it is declared can be
Int? X
System.Nullable x;
Useful, for example, for a field in db if the value is to be assigned to NULL, the current wording may be * * *. value = NULL; But such a child for the need to judge according to the situation is a value or empty, you need to write two lines of code to complete, very wordy, trouble, with nullable types convenient
Int? x = null;
int y = 10;
if (CHK). Checked = = True)
{
x = y;
}
Else
{
x = null;
}
***. Value = x;

6.:: Operator:: operator
A very interesting thing, which introduces the global keyword, and see if it's okay.


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.