C #3.0 New Features

Source: Internet
Author: User

Http://developer.51cto.com/art/200908/148353.htm

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 is still in PDC (ProfessionalProgramMember Conference) released a preview version, so eager programmers can see some of the expected Visual C #3.0 features, which 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

New Feature in Visual C #3.0: implicit type 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:

 
  
  
  1. 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:

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

TheCodeSet intarr to 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.

Visual C #3.0 new feature: anonymous Variables

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:

 
  
  
  1. 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:

 
 
  1. Class_ Anonymous1
  2. {
  3. Private String_ Hair ="Black";
  4. Private String_ Skin ="Green";
  5. Private Int_ Teeth = 64;
  6. Public StringHair {Get{Return_ Hair ;}Set{_ Hair = value ;}}
  7. Public StringSkin {Get{Return_ Skin ;}Set{_ Skin = value ;}}
  8. Public IntTeeth {Get{Return_ Teeth ;}Set{_ Teeth = value ;}}
  9. }

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:

 
  
  
  1. VaR Frankenstein =New{Hair ="Black", Skin ="Green", Teethcount = 64}

Visual C #3.0 new feature: 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:

 
  
  
  1. Public Static IntToint32 (This StringS)
  2. {
  3. ReturnConvert. toint32 (s );
  4. }

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:

  
  
  1. StringS ="1";
  2. IntI = S. toint32 ();

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

New Feature in Visual C #3.0: object and collection Initialization

C #3.0 is expected to allow you to include an initialization character to specify the initial values of a newly created object or collection. This allows you to combine the Declaration and initialization step by step.

For example, you can define the coordinate class as follows:

  
  
  1. Public ClassCoordinate
  2. {
  3. Public IntX;
  4. Public IntY;
  5. }

You can use an object initialization character to declare and initialize a coordinate object, just like this:

 
  
  
  1. VaR mycoord =NewCoordinate {x = 0, y = 0 };

Maybe you want to ask, why not do it like below?

  
  
  1. VaR mycoord =NewCoordinate (0, 0 );

Note: I have never declared a constructor for my class that accepts two parameters. In fact, using an object initialization operator to initialize an object is equivalent to calling a non-parameter (default) constructor and assigning values to the relevant amount.

Similarly, in C #3.0, you can easily assign a value to the collection in a more concise way, as shown in the following code of C #2.0:

  
  
  1. Listanimals =NewList ();
  2. Animals. Add ("Monkey");
  3. Animals. Add ("Donkey");
  4. Animals. Add ("Cow");
  5. Animals. Add ("Dog");
  6. Animals. Add ("Cat");

Can be shortened:

  
  
  1. Listanimals =NewList {"Monkey","Donkey","Cow","Dog","Cat"};

Visual C #3.0 new feature: Lambda expression: thick coffee with anonymous method

C # 1.x allows you to write code segments in the method. You can use the delegate to call the code easily. Delegation is undoubtedly useful and can be used in any framework. However, in many instances, you must declare a method or a class to use it. Therefore, in order to give you a simpler and more concise encoding method, C #2.0 allows you to use anonymous methods to replace standard calls to delegate. The following code can be seen in. net1.1 or earlier versions:

 
 
  1. ClassProgram
  2. {
  3. Delegate VoidDemodelegate ();
  4. Static VoidMain (String[] ARGs)
  5. {
  6. Demodelegate mydelegate =NewDemodelegate (sayhi );
  7. Mydelegate ();
  8. }
  9. VoidSayhi ()
  10. {
  11. Console. writeline ("Hiya !! ");
  12. }
  13. }

In C #2.0, to use the anonymous method, you must rewrite the Code as follows:

 
 
    1. class Program
    2. {
    3. DeleGate void demodelegate ();
    4. static void main ( string [] ARGs)
    5. {
    6. demodelegate mydelegate = DeleGate ()
    7. {
    8. console. writeline ( "hiya !! " );
    9. };
    10. mydelegate ();
    11. }
    12. }

Although the anonymous method takes a further step towards method-based delegate calling, lambda expressions allow you to write anonymous methods in a more concise and functional format.

You can use a Lambda expression as a parameter list to write code, followed by =>, followed by an expression or statement. The above code can be replaced with the following code:

  
  
  1. ClassProgram
  2. {
  3. Delegate VoidDemodelegate ();
  4. Static VoidMain (String[] ARGs)
  5. {
  6. Demodelegate mydelegate = () => console. writeline ("Hiya !! ");
  7. Mydelegate ();
  8. }
  9. }

Although lambda expressions are more concise, they are actually a functional superset of anonymous methods. Specifically, lambda expressions provide the following additional functions:

· They allow parameter types to be inferred. The anonymous method requires you to clearly state the status of each type.

· They support Query expressions or C # statements.

· They can be seen as data using the expression tree. This cannot be done using an anonymous method.

Visual C #3.0 new feature: query expression

This feature allows you to use SQL statements in a similar style in C #, also known as LINQ (integrated language query ).

For example, you can describe your data as follows:

 
  
  
  1. UblicClassCoordinate
  2. {
  3. Public IntX;
  4. Public IntY;
  5. }

In C #, you can easily declare the logical equivalent of a database table as follows:

  
  
  1. // Use object and collection initializers 
  2. Listcoords = ...;

Now, your data can be used as a collection to implement ienumerable. You can easily query data as follows:

 
  
  
  1. VaR filteredcoords =
  2. From CInCoords(Coords is equivalent to a set of objects, such as a table. Each record in a table is equivalent to an object, and this is also equivalent to foreach)
  3. Where X = 1
  4. Select (C. X, C. Y)

In the preceding SQL format, "from", "where", and "select" are Query expressions. Some features of C #3.0, such as anonymous type and extension method, are used, implicit local variables. In this way, you can use the SQL format to integrate unrelated data to work together.

Each query expression is actually converted into a C # call, for example:

  
  
  1. Where X = 1

Will be converted:

 
  
  
  1. Coords. Where (C => C. x = 1)

As you can see, this looks like a terrible Lambda expression and extension method. C #3.0 has many other Query expressions and rules about them.

Visual C #3.0 new feature: Expression Tree

C #3.0 contains a new type that allows expressions to be used as runtime data. This type, system. Expressions. expression, is just a re-expression of a Lambda expression in the memory. The result is that your code can modify and check lambda expressions at runtime.

The following is an example of an Expression Tree:

 
  
  
  1. Expressionfilter = () => console. writeline ("Hiya !! ");

Using the expression tree method above, you can use various attributes in the filter variable to check the content of the tree.

Conclusion

C #3.0 provides some new features that make it easier for you to complete the work of a programmer and architecture designer. It also maintains a rigorous and clear structure of the programming language.

C #3.0 is still in its infancy and will grow up in the next few months, but what it can change is close to its powerful backing.. NET Framework. Its architecture and design model deserve your attention.

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.