SOURCE Reference: Https://docs.microsoft.com/zh-cn/dotnet/csharp/whats-new/csharp-7
"VS2017 Support"
1.out variable (out variables)
Before the out parameter needs to be declared in advance outside the calling method, c#7.0 allows the out parameter to be declared in the method parameter pass.
Old wording:
int num; if (int.) TryParse ("123" out num)) { Console.WriteLine (num);} Else { Console.WriteLine ("Could not parse! " );}
New wording:
if (int.) TryParse ("123"outint num)) { Console.WriteLine (num);} Else { Console.WriteLine ("Could not parse! " );}
Out parameter declaration in method supports VAR implicit type
2. Tuples (tuples)
A method with multiple return types is worth the solution, using tuples.
Old wording:
Static voidMain (string[] args) { vardata =Getfullname (); Console.WriteLine (data. ITEM1); Console.WriteLine (data. ITEM2); Console.WriteLine (data. ITEM3);}Statictuple<string,string,string>Getfullname () {return Newtuple<string,string,string> ("a","b","C");}
New wording:[need to get System.valuetuple's reference via Nutget]
Static voidMain (string[] args) { vardata =GetFullName1 (); Console.WriteLine (DATA.A); Console.WriteLine (DATA.B); Console.WriteLine (DATA.C);}Static(stringAstringBstringc) GetFullName1 () {return("a","b","C");}
Deconstructed tuples Get return Values (multiple):
Static voidMain (string[] args) { //use Var to define a destructor tuple(stringAstringBstringc) =GetFullName1 (); Console.WriteLine (a); Console.WriteLine (b); Console.WriteLine (c);}Static(stringAstringBstringc) GetFullName1 () {return("a","b","C");}
3.pattern matching (matching mode)
Assign a value directly when judging the type.
Old wording:
Object 1 ; if is int // is judgment { int num1 = (int// split intTen // Add ten // Output }
New wording:[consider using this notation when using reflection]
Object 1 ; if is int num1)// to determine that obj is of type int, directly assigned to NUM1 {int; // Output }
New gameplay for switch:
Static DynamicADD (Objecta) { Dynamicdata; Switch(a) { Case intB:data= b++; Break; Case stringC:data= C +"AAA"; Break; default: Data=NULL; Break; } returndata;}
c#7.0 new Features