c#3.0 new Features

Source: Internet
Author: User

  

1. Initializer: className variableName = new ClassName (property1=value1 ...);

2.var can declare a variable of no type, change the data type according to the difference of the assignment

3. Anonymous method, if a class with less, you can omit the class name, directly create the instantiation

4. Extension methods: Support for adding methods to the base class

// anonymous method Examples Static void Main (string[] args) {    varnew"Jack"15  "man"  };            Console.WriteLine (class1.name+class1.age+class1.gender);              for (;;) ;}

Another example

  Public Static classstringextension {//extension class name, must be static class         Public Static stringExtensionfunction ( This stringInput) {//must have a parameter that represents the class instance that called the extension method           return "Hello"+input; }    }    classProgram {Static voidMain (string[] args) {            stringstr ="This is a string"; Console.WriteLine (str. Extensionfunction ());//output Hello this is a string             for (;;) ; }    }

LANMDA-expression

Lanmuda expression: is used to simplify the anonymous method, consisting of three parts: 1. Parameter list in parentheses (untyped, can infer argument) 2.=> operator 3.c# statement

    Delegate intCalculatedelegate (intAintb);//building two parameter delegates    classProgram {Static voidcalculatefunction (Calculatedelegate cal) {intA = -, B = -;        Console.WriteLine (Cal (b)); }    Static voidMain (string[] args) {Program.calculatefunction (A, b)=>A+B);//OutputProgram.calculatefunction ((A, B) and A * b);//Output 10000calculatedelegate C; C= (A, b) = + A + b;//equivalent to a build functionConsole.WriteLine (C (Ten,Ten));//Output             for (;;) ; }    }

c#3.0 new Features

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.