c#6.0 characteristics (come to onlookers)
1. Automatic attribute Enhancement
1.1. Automatic attribute initialization (initializers for auto-properties)
C#4.0 under the resolute realization cannot.
How automatic attributes are initialized in c#6.0
As long as the contact with C # will certainly like this way. It's simple and convenient.
1.2. Read-only property initialization getter-only Auto-properties
Let's take a look at the way we used to do it.
public class Customer {public string Name {get;} Public Customer (String firstname,string lastName) { Name = firstName + "" + LastName; } }
And look at the c#6.0.
public class Customer {public string FirstName {get;} = "Aehyok"; public string LastName {get;} = "Kris"; }
And the first automatic property initialization is used in the same way.
2. Expression bodied function members
2.1 Using lambda as function body expression bodies on Method-like members
Public point Move (int dx, int dy) = + new Point (x + dx, y + dy);
Let's take a simple example: a function with no return value
public void Print () = Console.WriteLine (FirstName + "" + LastName);
2.2. Lambda expression as an attribute expression bodies on Property-like function members
public override string ToString () { return FirstName + "" + LastName; }
Now in C#6
public class User {public string FirstName {get; set;} public string LastName {get; set;} public override string ToString () = string. Format ("{0}--{1}", FirstName, LastName); public string FullName = + FirstName + "" + LastName; }
3. Reference static class using static
You can specify a static class in the using, and then you can use the static member directly in the subsequent code
4, empty value judgment null-conditional operators
Look directly at the code and run the results
The result can be found to return null, no longer as cumbersome as before to judge Null.
5. String embedding value
Embed a value in a string
The way it has been used before is
Now we can simply stitch in the following way
6. nameof Expression nameof expressions
In the method parameter check, you may often see this kind of code (used less, this time also learned)
public static void Addcustomer (Customer customer) { if (customer = = null) { throw new ArgumentNullException ("Customer"); } }
There is the customer is our handwritten string, in the name of the customer, it is easy to forget the following string, c#6.0 nameof help us solve this problem, see the new wording
public static void Addcustomer (Customer customer) { if (customer = = null) { throw new ArgumentNullException (nameof (Customer)); } }
7. Indexed object initializer Index initializers
The initialization of objects directly through the index, the original can actually be implemented
In this way, only three elements in the dictionary can be found, so only the three indexes can access the amount, and other types of objects and collections can be initialized in this way, not listed here.
8. Abnormal filter (Exception filters)
Let's take a look at a transplant method.
Try { var numbers = new Dictionary<int, string> {[7] = "Seven", [9] = "Nine", [[] = "Thirteen"}; } catch (ArgumentNullException e) { if (E.paramname = = "Customer") { Console.WriteLine ("Customer can not be null");} }
Another usage is given in Microsoft's documentation, which is thrown to the previous caller when the log record fails
private static bool log (Exception e) {//// handle some log return false; } static void Main (string[] args) { try {/// } catch (Exception e) {if (! Log (e)) { } } console.readline (); }
9, catch and finally await--await in catch and finally blocks
In c#5.0, the await keyword cannot appear in catch and finnaly blocks. And in 6.0,
Try { res = await resource.openasync (...);//You could does this .... } catch (resourceexception e) { await Resource.logasync (res, E);//Now you can does this ... } finally {
if (res! = NULL) await Res. Closeasync (); ... and this.
10, parameterless struct constructor--parameterless constructors in structs
(Excerpt from: http://www.cxyclub.cn/n/63739/)
c#6.0