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