c#7.0 new Features

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.