Those things in C # 6.0

Source: Internet
Author: User

These two days mid-term exams do not have time to see Connect (), live, it is a pity, after the exam to see Connect (); Sum up the new things in c#6.0.

Automatic property initialization (initializers for auto-properties)

That's what we wrote before.

Adding a background field to a default is not so bad, now we can write it like this

Initialization of Read-only properties (Getter-only auto-properties)

Like the user ID of this read-only property, which we wrote before

Now we can write that.

A read-only property can be assigned to a constructor in the same way as a field labeled readonly .

Using lambda as the function body (Expression bodies on Method-like members)

Usually there are some short and short code, but we have to put them in two brackets, now we can write

lambda expression as attribute (expression bodies on Property-like function members)

This usage can also be used for attributes

String embedding value (string interpolation)

This is a strange term, see an example of the above string. Format can actually be written like this, not only easy to write, but also very readable.

If you want to use the format control, and the same as before to add it.

Using static class (using Statics)

If a static class is a bunch of methods, such as Math , you can call his static method without writing the class name.

Some people say that this is a destructive object-oriented suspicion, I think this allows C # in the functional programming of the further, as to what exactly, time would tell.

It is worth mentioning that this using will also introduce an extension method, before the using System.Linq will introduce all extension methods under this namespace, if only a subset (such as Enumerable) is required, This usage can be very convenient.

Null judgment (Null-conditional operators)

This type of writing is believed to have a lot of friends used, often for an empty question to make the code very ugly, such as the video of the

For example, we want to get the length of a list

It's disgusting, in c#6.0, we can write this.

One of the rules of this operator can be seen here: If the object is empty, the value of the entire expression is empty.

Subsequent member accesses are not limited to methods, but can also be attributes, indexers, and so on.

To give an example of a practical application, when triggering an event, it is often seen that a delegate is always judged to be empty before it is called

Now we can do this.

If propertychanged is null, then this sentence will do nothing.

nameof expression (nameof expressions)

This code is often used when checking the method parameters

There is that role is our handwritten string, when renaming a role, it is easy to forget the following string, c#6.0 solved this problem, look at the new wording

Indexed object initializer (index initializers)

Object initializers are already available in c#3.0, and the c#6.0 object initializer adds support for indexers, making it easy to initialize things like dictionaries.

This is an example of a json.net use

Exception filter (Exception filters)

This has been added in VB and F # in the early features, see the usage

Another usage is given in Microsoft's documentation, which is thrown to the previous caller when the log record fails

Await in catch and finally (await in catch and finally blocks)

This is another anomaly-related feature that allows us to wait for an async method in catch and finally, looking at Microsoft's example

struct constructor without parameters (parameterless constructors in structs)

In previous versions of C #, the struct is not allowed to have an parameterless constructor, which is allowed in c#6.0, but it is important to note that the constructor is called by the new structure, and the default is not called

Take a look at this example

The first is a struct, created in two different ways, and then output

Missing features

Primary constructor and out parameters define variable that is missing, there is a cut off the statement, Microsoft is really tangled.

Details: Changes to the language feature set

Overall feeling c#6.0 added is the syntax of sugar, the language itself does not have a lot of new concepts, the addition of syntactic sugar will let C # programmers write code more beautiful, more productive, I hope C # more do better!

Those things in C # 6.0

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.