C # 3.0 New language features and improvements (i)

Source: Internet
Author: User

Introduction

About the characteristics of the c#3.0, the garden has a lot of, maybe everyone is very familiar with, although I have used in development, but I still need to record, summed up a bit. It is also the basis behind writing LINQ knowledge. Want to be interested in friends, can see.

C # 3.0 new language features and improvements include:

    • Automatic properties
    • Implicitly typed Local variables
    • Anonymous type
    • Object vs. Collection initializer
    • Extension methods
    • Lambda expressions and lambda expression trees
1. Automatic attributes

Automatic attributes avoid the logic of declaring private variables and get/set when we write entity classes, instead, the compiler automatically generates a private variable and the default Get/set action for you.

In. Net2.0 we define a product class in this way.

 Public classProdcut {Private string_productid;  Public stringProductid {Get{return_productid;} Set{_productid =value;} }        Private string_prodcutname;  Public stringProdcutname {Get{return_prodcutname;} Set{_prodcutname =value;} }    }

Automatic attribute notation:

     Public class prodcut    {        publicstringgetset;}          Public string Get Set ; }    }

Like the empty Get/set attribute above, the class will automatically generate a member variable and a public get/set, or you can set the access level of Get/set, as follows

    Publicclass  prodcut    {        publicstring get  Privateset;}          Public string Private Get Set ; }    }
2. Implicitly typed Local variables

C # 3.0 introduces the New keyword Var, which can be used to override the original type name when declaring a local variable, and this declaration is considered an implicit type local variable declaration.

vari =5;varj ="Hello World";varK =50.36;varX//Errorvary =NULL;//Errorvarz = {1,2,3};//Error

Remove the error code, we debug the state, view the data type

Implied type local variable essentials:

    • The var local variable must be initialized.
    • the var keyword can instruct the compiler to infer the actual data type through the initialization section on the right.
    • The type of an expression at compile time cannot be a null type.
    • The initialization statement can be an expression, cannot be empty, and the compilation can determine the type, and once initialized, only this type can be stored.
    • VAR can only declare local variables, can not be global, and can also be used in foreach, for, using, and other statements.
    • An initialization session cannot be itself an object and or a collection initializer, but it can contain an object or a new expression of the initializer.
3. Anonymous type

1. What is an anonymous type?

An anonymous type, as the name implies, is a type that has no name (the surface is not visible and the class name is automatically generated by the compiler).

2. What is the role of anonymous type?

When you need to define a class to encapsulate some related data without requiring any associated methods, events, and this class does not need to be reused in the project, we can also consider using anonymous types to simplify our operations.

3. Usage

var New " Swich " " http://www.cnblogs.com/liujie2272 " };

Above the anonymous type, the editor will consider the user equivalent to:

 Public class User    {        publicstringgetset;}          Public string Get Set ; }    }

Array Anonymous Type:

var New []   {         new"zhangsan"  ,         New  "LiSi" }  ;

Anonymous type essentials:

    • You can use the New keyword to invoke an anonymous object to create an anonymous object.
    • Anonymous types inherit from System.Object.
    • Types of anonymous types are some read-write properties that are inferred by the compiler based on the initializer.
4. Object and Collection initializers

1. Object initializer

In. Net 2.0, which is very dependent on properties, when generating object instances and using objects, we write:

New"swich""http://www.cnblogs.com/liujie2272 ";

In. Net 3.0, the object initializer consists of a series of member objects whose objects must be initialized, separated by commas, and enclosed with {}. we can write this:

New " Swich " " http://www.cnblogs.com/liujie2272 " };
  New List<user>            {               new"swich""/http/ www.cnblogs.com/liujie2272"  },               new"swich  "http://www.cnblogs.com/liujie2272 " }            ;

2. Set initializer

The collection initializer consists of a series of collection objects, separated by commas and enclosed with {}.

list<intnew list<int,51, in. };

C # 3.0 New language features and improvements (i)

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.