ASP. MVC-1, Pre-Knowledge reserve (c#3.0 new feature)

Source: Internet
Author: User

It is particularly important to understand the new syntactic features of c#3.0 before learning ASP. Because the new features of c#3.0 in the MVC project will greatly improve our development efficiency, and you will see C # everywhere in the MVC project 3.0 new features of the figure.

c#3.0 new Features
    • Automatic properties
    • implicitly-typed Var
    • Object initializers and collection initializers
    • Anonymous class
    • Extension methods
    • Lambda expression
Automatic properties

This concept is simple and it simplifies our. NET when writing a bunch of private members + properties programmatically, we only need to declare a property as follows, and the compiler will automatically generate the required member variable.

Basic usage:

  Public classUser { Public intId {Get;Set; }  Public stringName {Get;Set; }  Public intAge {Get;Set; }  PublicAddress Address {Get;Set; } }

Before c#3.0, we did this to implement the properties:

Private int ID;          Public int Id        {            get            {                return  ID;            }             Set             {                = value;            }        }
Implicitly inferred types

This name may be strange to you, but the keyword var should be used, in C # when declaring an object with Var, the compiler automatically infers the type of the local variable based on its assignment statement. After the assignment, the type of the variable is determined and can no longer be changed. In addition, the VAR keyword is also used for anonymous class declarations.

Application: The main purpose of VAR is to represent the result of a LINQ query. This result may be an object of type objectquery<> or iqueryable<>, or it may be an object of a simple entity type. Using VAR to declare this object can save a lot of time in code writing.

var New Customer ();

Object initializer

One way to construct an object in. NET2.0 is to provide an overloaded constructor, and the second is to generate an object with the default constructor and then assign a value to its properties. In. net3.5/c#3.0 we have a better way to initialize an object. That is, using the object initializer. This feature is also a basis for anonymous classes, so it is introduced before anonymous classes.

Basic usage:

  New 1 " ZOUQJ "  ;

Anonymous class

With the introduction of the previous initialization, the anonymous class is simple. We can use new {object initializer} or New[]{object, ...} To initialize an anonymous class or an array of indeterminate types. Objects for anonymous classes need to be declared with the VAR keyword. Example code:

var New 1 "  "a" ;

Application:

When directly using select New {object initializer}, the syntax is to return the results of a LINQ query to an anonymous class.

Lambda An expression

The essence of a lambda expression is an anonymous function, which is named after the lambda calculus in mathematics, and directly corresponds to the abstraction in which it is an anonymous function, that is, a function without a function name.

 delegate  int  Adddel (int  a,int  b); //             define a delegate   #region  lambda Adddel fun = delegate (int A, int b) {return a + B;};            anonymous function//console.writeline (Fun (1, 3));            The lambda parameter type can be implicitly inferred, and you can omit the type Lambda essence is the anonymous function Adddel Funlambda = (A, b) + A + B;            List<string> STRs = new list<string> () {"1", "2", "3"}; var temp = STRs. FindAll (s = = Int.            Parse (s) > 1);            foreach (var item in temp) {Console.WriteLine (item);            }//console.writeline (Funlambda (1,3));   #endregion  

ASP. MVC-1, Pre-Knowledge reserve (c#3.0 new feature)

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.