Microsoft released the new VS 2015 yesterday. And then there's a lot of stuff .... NET new version of ASP. NET new version ... Wait a minute.. Too many. Not really digested.
Share some new features of the new c#6.0 that were released yesterday ...
Of course.. I have not used--, mainly refers to a foreign one article. Very detailed, good English can see for themselves
Https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6
First of all
Automatic Property Initialization Enhancements
public class customer{Public string First {get; set;} = "Jane"; public string Last {get; set;} = "Doe";}
public class customer{Public string First {get;} = "Jane"; public string last {get;} = "Doe";}
public class customer{ //Read-only property public string Name {get;}; Assignment in initialization method is possible ~ Public Customer (string First, string last) { Name = first + "" + Last; }}
It is not feasible in c#5.0 such as:
The method function supports lambda notation as follows:
public void Print () = Console.WriteLine (First + "" + last);
importing a static class directly, rather than using a static class name in your code, is supported for direct import of the namespace as follows:
Static import consoleusing static system.console;using static system.math;using static System.dayofweek;class program{ static void Main () { //Direct use method without Console.WriteLine WriteLine (Sqrt (3*3 + 4*4)); WriteLine (Friday-monday); }}
Extension method, (this is not very understand, explanation is not good please forgive)
In the main class, statically import the type you want to extend, and then write the method you need to extend:?.. --Don't understand.
Using static System.Linq.Enumerable; The type, not the Namespaceclass program{ static void Main () { var range = Range (5, +); Ok:not extension var odd = Where (range, i = i% 2 = = 1);//Error, not in scope var even = range. Where (i = = i% 2 = = 0); Ok }}
The non-empty syntax sugar is as follows:
Int? First = customers? [0]. Orders.count ();//The above notation is equivalent to the following int? First = (Customers! = NULL)? Customers[0]. Orders.count (): null;
String Formatting new gameplay:
The original, we need this. var s = String.Format ("{0} is {1} year{{s}} old", P.name, P.age);//c#6.0, directly as follows: var s = $ "{P.name} is {p.age} Year{{s}} old ";//can even be arbitrary in {}. (Words do not: expression or what the hell. --,) anyway: var s = $ "{p.name,20} is {p.age:d3} year{{s}} old"; var s = $ ' {P.name} is {p.age} year{(p.age = 1?) "": "S")} old ";
Index initialization:
var numbers = new Dictionary<int, string> { [7] = "Seven", [9] = "Nine", [[] = "Thirteen"};
Exception Filters:
If when the expression in parentheses evaluates to run in a True,catch block, the exception persists.
(--And yet I don't understand it.) Ask the great God to explain ...)
try {...} catch (MyException E) when (Myfilter (e)) { ...}
Catch blocks that can be asynchronously awaited:
Resource res = null;try{ res = await resource.openasync (...); You could does this. ...} catch (Resourceexception e) { await Resource.logasync (res, e); Now the can do ...} finally{ if (res! = null) await res. Closeasync (); ... and this.}
This is basically the end,--the level is limited. Bad and imperfect place please point out. Lest you mislead us.
[Dry goods to attack] c#6.0 new Features