c#6.0 and 7.0 new features introduction record

Source: Internet
Author: User

C # History

Reference Address: https://www.cnblogs.com/ShaYeBlog/p/3661424.html

6.0 new Features

1. String stitching optimization

Syntax format:$ "string {parameter }"

Explanation: Begins the string with the $ sign, with the {} for the argument, can accumulate multiple parameters

For example: var results = $ "Hello {Name}";

2.null detection operator

Syntax format: " ?. 】

Explanation: The object value to be judged followed? The sign symbol directly determines whether it is empty, and then the condition is not empty after you need to perform the "." After execution

For example:

  New Program ();                Console.WriteLine (Pro?) public   string  getcnblogssite ()        {            return' Http://www.cnblogs.com/yinrq " ;        }

3. Automation Properties

Syntax format: attribute{get;set}= "";

Explanation: An assignment can be performed directly after an object property. Like a normal assignment.

For example: public string name {get;set}= "Zhang San";

4. Expression Method Body

Syntax format: = "

Explanation: One sentence of the method body, you can use the arrow function, do not need curly braces, more convenient

For example: Public ActionResult index () = View ();

5. Referencing static classes

Explanation: You can refer to a static class directly

Example:using static System.Math;

6. Object initializers with Indexes

Explanation: A form like a dictionary can be initialized directly by an index.

For example: var dic = new Dictionary<int, string> {[0]= "first value", [1]= "Second Value"};

Using await in 7.catch and finally

Explanation: In 5.0, the await is not used in catch and finnaly blocks, and the limit is removed in 6.0.

8.out parameter notation

For example: Int.   TryParse ("123", out int x); You can define it directly when you use it.

9. Structs can contain parameterless functions

Explanation: In the previous struct it was not possible to include an explicit parameterless constructor, and now it can be

7.0 new features

1. Tuples

Syntax format: Tuple

Explanation: You can create a different type into a collection and then return it, using item1,item2 .... You think this is the thing, wrong to know this is 4.0 of things, and now 7.0 has been optimized for us. But you need to quote system.valuetuple.

For example:

  Static voidMain (string[] args) {            varData=Getfullname (); Console.WriteLine (DATA.A); //available names get to valueConsole.WriteLine (DATA.B);            Console.WriteLine (DATA.C);        Console.ReadLine (); }        //method is defined as multiple return values and named        Private Static(stringAstringBstringc) getfullname () {return("a","b","C"); }
View Code

Another example:

Static voidMain (string[] args) {           //defining a destructor tuple(stringAstringBstringc) =Getfullname ();            Console.WriteLine (a);            Console.WriteLine (b);            Console.WriteLine (c);        Console.ReadLine (); }        Private Static(stringAstringBstringc) getfullname () {return("a","b","C"); }
View Code

In addition to the original form of writing

// Tuple data Creation         Public tuple<intstring> gettuple ()        {                     return tuple.create (1 " 001 " );         // get tuple values        var result = gettuple ();        Console.WriteLine (Result. ITEM1);        Console.WriteLine (Result. ITEM2);
View Code

2. Matching mode

2.1 Small expansion for IS

Format: A is type B

Explanation: Parameter A is not this type if it is assigned to B and then use B to do the operation

For example: Object A = 1;if (A is int c) Console.WriteLine (c);

Small expansion of 2.2switch

Format: Case Type A: and the case of the filter value type a when a<0:

Explanation: A case can be matched against a type and is conditionally filtered after it is matched by type

Changes to 3.ref

Explanation: Ref was previously used only as a parameter, and in 7.0 ref can be used as a local variable and as a return value.

4. Local functions

Explanation: A function similar to a local variable that can be accessed only in a particular function. A bit similar to the sense of the Func delegate can be used internally to define a local method for temporary use, the specific difference has not been cleared.

                int 1 ;                 int Add (int  c)                {                    return1;                }                Console.WriteLine (Add (a));

5. Expression method Body Extension

Explanation: In 6.0 a sentence method can be abbreviated, but the constructor, destructor, property accessors can not, in 7.0 to expand this

For example:

//expressions for constructor functions PublicCachecontext (stringlabel) = = This. Label =label;//the expression of the destructor function~cachecontext () = Console.Error.WriteLine ("finalized!");Private stringlabel;//Get/set expression of a property accessor Public stringlabel{Get=label; Set= This. Label = value??"Default Label";}

6. Improved numeric text syntax

Format: _

Explanation: Using the "_" divider in the middle of a running value is purely for readability. No other practical significance.

c#6.0 and 7.0 new features introduction record

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.