1. Automatic attribute default initialization
Using code
Public string Get Set " 001 ";
Compiler-Generated code:
Public classCustomer {[compilergenerated]Private stringKbackingfield ="Hello World"; PublicCustomer () { This. Kbackingfield ="Hello World"; } Public stringname{[compilergenerated]Get { return This.<name>K__backingfield; } [compilergenerated]Set { This. <name>k__backingfield =value; }}}
The compiler initializes the property when the constructor is instantiated.
2. Automatic read-only property default initialization
Using code
Public string Get " Hello World ";
Compiler-generated code
Private ReadOnly string public Customer () { this'helloworld' Publicstring Name1 { [compilergenerated] get Returnthis. K__backingfield;}}
The assignment of a read-only property can only be performed in the constructor
3. The function of the expression as the main body
Using code
Private Static void Test () = Console.WriteLine ("Hello world!! " ); Body Get (intintnew Body (12 + y);
Compiler-generated code
Private program.body Get (int x, int y) { new program.body (12 + y);}
If the function body has only one line of code, the function with the body of the expression is more concise
also supports the writing of asynchronous functions
Async void OutPut (intint,awaitnew Task () = Console.WriteLine ("Hello wolrd"));
4. The expression is the property of the principal (assigned value)
Using code
Public string " Frank ";
Compiler-generated code
Public string name{ "Frank";}}
You can see that this property is a read-only property
5. Static class Import
Using code
using Static System.Console; Private Static void Listcontacts (ienumerable<contact> contacts) { foreach (var in Contacts) { WriteLine ("{0,-6}{1,-6}{2,-20}{3,-10}", Contact. Id, contact. Name, contact. EmailAddress, contact. phonenum); } WriteLine ();}
Its stone can attack Jade, C # This is the Java of this relatively good grammatical characteristics of reference to come over, praise one.
Compiler-generated code
Private Static void Listcontacts (ienumerable<contact> contacts) { foreach (var in Contacts) { Console.WriteLine ("{0,-6}{1,-6}{2,-20}{3,-10}" , contact. Id, contact. Name, contact. EmailAddress, contact. phonenum); } Console.WriteLine ();}
6. Null condition operator
Using code
New Customer (); string name = Customer?. Name;
Compiling code
New Customer (); if NULL ) { string name = customer. Name;}
Can also and?? Combine them with
if (Customer?. Face ()?? false)
You can also use two combinations together to
Int
The purpose of this syntax sugar is to check for null before the object is used. If the object is empty, the variable is assigned a null value, so an int, or int, that can be empty is required in the example. If the object is not empty, the member of the calling object is evaluated and assigned to the variable.
7. String Formatting
String.Format Some inconvenient place is: Must enter "String.Format", use {0} placeholder, must order to format, this is error-prone.
var string. Format ("id:{0} name:{1} emailaddr:{2} phonenum:{3}", contact. Id, contact. Name, contact. EmailAddress, contact. Phonenum);
The new syntax
var contactInfo2 = $"id:{contact. Id} name:{contact. Name} emailaddr:{contact. EmailAddress} phonenum:{contact. Phonenum}";
Use more handy, praise!
The new format also supports direct assignment of any expression:
var contactinfo = $"Frank" : Contact. Name)} emailaddr:{contact. EmailAddress} phonenum:{contact. Phonenum}";
Analysis of c#6.0 grammatical sugars