New Features of C #6 ~,

Source: Internet
Author: User
Tags tojson microsoft c

New Features of C #6 ~,

Original article address

Mads Torgersen, Microsoft C # Program Manager, published a video describing the coming C # version -- C #6. Among the new features of C #6, Mads mainly mentions the getter-only attribute, the lambda arrow operator, and string interpolation.

First, Mads said that C #6 will not revise the design philosophy of C # and will provide many small features to help clean code.

Getter-only automatic attributes

C #6 allows the definition of unchanged automation attributes. For example, only the automation attributes of one getter method are supported:

public class Point
{
    public int X { get; }
    public int Y { get; }
    public int ReadWrite { get; set; }
}

The Getter-only automation attribute is read-only and can be assigned by the constructor.

  

This is not very clear. Versions earlier than C #6 also have this feature ~

String operation

The traditional C # Syntax of string interpolation is described by Mads as "a chaotic and error-prone ":

return String.Format("({0}, {1})", X, Y);

C #6 introduces a new string insertion Syntax:

return "(\{X}, \{Y})";
Lamba arrow operators (Lamba-arrow operator)

The new lambda arrow operator simplifies the definition of returning a single expression value:

public override string ToString() => "(\{X}, \{Y})";
public double Dist => Sqrt(X * X + Y * Y);

According to Mads, this will help reduce the typing of a large number of "Sample" codes.

Index initializer

In the current C #, the index calculator must assign values using a separate statement:

var result = new JObject();
result["x"] = X;
result["y"] = Y;

In C #6, an expression may be used to initialize the object:

var result = new JObject() { ["x"] = X, ["y"] = Y };

This will lead to a highly concise and readableToJSonMethod definition:

public JObject ToJson() => new JObject() { ["x"] = X, ["y"] = Y };
Null propagation operator (Null-propagating operators)

The Null condition operator is a new feature designed to check the null value. Therefore, it is not as follows:

if (json != null &&
    json["x"] != null &&
    json["x"].Type == JTokenType.Integer)

C #6 run a new? The computation follows the part of its expression. As long as it is applied to a non-null object, the above expression can be reduced:

if (json?["x"]?.Type == JTokenType.Integer)
  

In the above several features, Microsoft apparently absorbed the practices of the open-source community, such as Groovy, to make the code more concise ~

Exception filtering (Exception filtering)

If the capture expression meets a specific condition:

try
{ }
catch (ConfigurationException e) if (e.IsSevere)
{ }

Mads said this is a better catch-rethrow syntax, because in the past, when an exception was thrown again, the initial occurrence of the exception would be lost.

In the catch and finally blocksAwait

Finally, await can be used in catch and finally blocks, which increases the flexibility of error handling. Mads added that this feature was excluded from Versions earlier than C #6, because it was still unclear how this feature was implemented.

try
{ }
catch (ConfigurationException e)
{ 
    await LogAsync();
}
finally
{
    await CloseAync();
}
  

The recently released Xamarin. Studio 5.9 has added C #6 support, improved Sketches, and introduced new debugger viewer and other updates. C #6 will be officially released in Visual Studio 2015 and only available in the Community version. However, Xamarin. Studio also supports the new features of C #6.

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.