1. Automatic attributes (auto-implemented properties)
// the previous wording. net2.0 Private string _username; Public string username{ getreturn _username;} Set {_username= value;}} // now only for more than 3.5 public String_username {get; Set;}
2. implicitly-typed Var
It is the type in which the compiler has been able to determine the variable, the type is automatically inferred from the subsequent values, and the inferred type is replaced with Var at compile time
look at the following code: var New {name="Tom", age=", email="[Email Protected]" }; Console.WriteLine (V.name); Console.WriteLine (V.age); Console.WriteLine (V.email); Console.ReadLine (); // var v.name= "xiaoming", implicit type can only read, cannot be assigned value
3. object initializers and collection initializers
Object initializer
// Writing a demotest person = new demotest {name= "xiaoming", age=18,sex= "Boy"}; // notation two demotest person = new Demotest ("123") {name= "xiaoming", age=18,sex= "Boy"}; // // One is to use//to invoke the class's default constructor when calling the class 's default parameter constructor
Collection initializer
New List<demotes> {new demotest () {age=, name="xiaoming" , sex="boy"},new demotest () {age=, name=" Xiaohua ", sex="girl"} ;
4. extension Methods
Static void Main (string[] args) { List<stringnew list<string>() ; List. Contains// go To Definition }
These with down arrows are extended, then we go to the definition (F12) See, this extension is defined in the enumerable class, is not with the people think not
The same? In general, it goes to the List<t> class, so all extension methods are in the static class .
What is the main purpose of the extension method we want to write?
When a type has been written, you need to add some methods to this type, but add this method can not modify the type, can not modify the source code of this class, then you will want to directly modify the source code is not the line, If. net2.0 Previous version list There are some methods that are a version, if you modify a problem that might be incompatible
Now let's write an extension method ourselves.
classProgram {Static voidMain (string[] args) { //add an extension method to Newclass, outputNewclass Newclass =NewNewclass (); Newclass. Age= -; Newclass. ShowMessage (); Console.readkey (); } } Public classNewclass { Public stringName {Get; Set; } Public intAge {Get; Set; } Public stringEmail {Get; Set; } Public voidSayHello () {Console.WriteLine ("Hello"); } } //1. Create a static class Public Static classDemotest { Public Static voidShowMessage ( ThisNewclass Newclass)//This must be added here, not the usual method.{Console.WriteLine (newclass). Age); } }
Let's add an extension method to string:
classProgram {Static voidMain (string[] args) { stringstr ="Hello"; intresult =Str. GetLength (); Console.WriteLine (result); Console.readkey (); } } //1. Create a static class Public Static classDemotest { Public Static intGetLength ( This stringstr) { //extension methods cannot access private members in a type returnStr. Length; } }
Next we use the anti-compilation to look:
The extension method simply looks like a method of an object, and from the decompile we can see that the method is actually not the object at all, in fact it is called a method in a static class.
Automatic attributes in C #, implicit type Var, object initializers and collection initializers, extension methods