Visual C #3.0 overview of new features (1)

Source: Internet
Author: User

After Visual Studio 2005 and C #2.0 were released, Microsoft immediately showed the expected next generation of C #: C #3.0. Although C #3.0 is not standardized, Microsoft released a preview version at the PDC (professional programmer Conference), so eager programmers can see some of the expected features, this is also the main content discussed in this article:

· Implicit local variables

· Anonymous Variables

· Extension Method

· Object and collection initialization characters

· Lambda expressions

· Query expression

· Expression Tree

Implicit local variables

C #3.0 introduces a new keyword named "Var ". VaR allows you to declare a new variable. Its type is implicitly inferred from the expression used to initialize the variable. That is to say, the following expression is a valid format:

VaR I = 1;

This line uses 1 to initialize the variable I. Note that I is strongly typed to an integer. It is not an object or VB6 variable, nor is it loaded with other objects or variables.
To ensure the strong type of variables declared using the VaR keyword, C #3.0 requires that you place the value assignment (initialization) in the same line as the declaration (Declaration. Similarly, the initialization character must be an expression, not an object or collection initialization character, or null. If multiple specifiers exist for the same variable, they must be considered of the same type during compilation.

On the other hand, the implicit type array can use a different format, as shown below:

VaR intarr = new [] {1, 2, 3, 4 };

The above line of code declares intarr as int [].

The VaR keyword allows you to use anonymous instances. Therefore, these instances are static instances. Therefore, when you create an instance that contains a set of data objects, you do not need to define a class that supports both this structure and data in a static type variable.

Anonymous variable

C #3.0 allows you to create a class instance flexibly without having to write the class code first. So you can write the code like this:

New {Hair = "black", skin = "green", teethcount = 64}

The last line of code, with the help of the New Keyword, creates three types of attributes: hair, skin, and teethcount. In this way, the C # compiler creates a class as follows:

Class _ anonymous1
{
Private string _ hair = "black ";
Private string _ Skin = "green ";
Private int _ teeth = 64;
Public String hair {get {return _ hair;} set {_ hair = value ;}}
Public String skin {get {return _ skin;} set {_ Skin = value ;}}
Public int teeth {get {return _ teeth;} set {_ teeth = value ;}}
}

In fact, if another anonymous type that meets the same name and type order is created, the compiler will intelligently create only one anonymous type to support two instances. Similarly, Because instances are simple instances of a class, they can be exchanged because the types are actually the same.

Now you have this class, but you still need something to support an instance of the above class. This is the role of the "Var" keyword. It gives you a static instance with more than one anonymous variable. Here is an example of a simple and easy-to-use anonymous type:

VaR Frankenstein = new {Hair = "black", skin = "green", teethcount = 64}

Extension Method

The extension method allows you to use additional static methods to expand various types. However, they are very limited, and can only be used as an alternative when the instance method is insufficient.

An extension method can only be declared in a static class and marked with the keyword "this" placed in the first parameter of the method. The following is an example of a valid extension method:

Public static int toint32 (this string S)
{
Return convert. toint32 (s );
}

If a static class containing the above methods is introduced using the "using" keyword, The toint32 violation will appear in the existing type (although it is lower than the existing instance method priority ), you can compile and execute the Code as follows:

String S = "1 ";
Int I = S. toint32 ();

This allows you to fully enjoy various built-in or defined type Extension features and add new methods to them.

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.