C # Those Things In 6.0,

Source: Internet
Author: User

C # Those Things In 6.0,

The two-day mid-term exam does not have time to watch Connect (); it is a pity that after the exam, I completed the Connect (); I summarized the new things in C #6.0.

Initializers for auto-properties)

This is what we wrote before.

Is it uncomfortable to add a background field to a default value? Now we can write it like this.

Read-only property initialization (Getter-only auto-properties)

Read-Only attributes such as user IDs, which we previously wrote

Now we can write

The read-only attribute can be matched.ReadonlyIn the constructor.

Use Lambda as the function body (Expression bodies on method-like members)

There are always some short and concise code, but we have to put them in two brackets. Now we can write this

Lambda expressions are used as attributes (Expression bodies on property-like function members)

This method can also be used for attributes.

String interpolation)

This is a bit strange. I can see the example above.String. FormatIn fact, it can be written in this way, which is not only easy to write, but also very readable.

If you want to use format control, you can add it as before.

Using static)

If a static class contains a bunch of methods, suchMathYou can directly call the static method without writing the class name.

Some people say this is a suspicion of undermining object-oriented programming. I think this makes C # go further in functional programming. How is time will tell.

It is worth mentioning thatUsingThe extension method will also be introduced.Using System. LinqAll the extension methods in the namespace will be introduced.Enumerable.

Null-conditional operators)

I believe many of my friends have used this method. It is often difficult to make code for a question of whether it is empty, such as the one in the video.

For another example, we want to obtain the length of a list.

This is really disgusting. in C #6.0, we can write it like this.

We can also see a rule for this operator: if the object is null, the value of the entire expression is null.

Access by subsequent members is not limited to methods, but can also be attributes or indexers.

For a practical example, this method is often used when an event is triggered. Before a delegate is called, it is always necessary to determine whether it is null.

Now we can do this

IfPropertyChangedIsNullThen, nothing will be done.

Nameof expression (nameof expressions)

This code is often seen during method parameter checks.

The role in it is a handwritten string. When we rename the role, it is easy to forget the string below. C #6.0 solves this problem and looks at the new writing method.

Index initializers)

The object initializer is already available in C #3.0, and the object initializer in C #6.0 has added support for the indexer, so that something like the dictionary can be easily initialized.

This is an example of Json. NET usage.

Exception Filter)

This feature has been added in VB and F # for usage.

The Microsoft document also provides another usage. This exception will be thrown to the caller at the previous layer when the log record fails.

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

This is another exception-related feature, so that we can wait for the Asynchronous Method in catch and finally. Let's look at the Microsoft example.

Parameterless constructors in structs)

In the previous version of C #, struct types are not allowed to have non-argument constructors. in C #6.0, struct types are allowed. Note that, the constructor is called through the structure obtained through new, but not through default.

Let's look at this example.

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

Missing features

The variables defined by the Primary constructor and out parameters are gone. It is said that the cut is gone, and Microsoft is really entangled ..

Details: Changes to the language feature set

The general feeling is that C #6.0 is more about syntactic sugar, and there are not many new concepts in the language itself. The addition of syntactic sugar will make the code written by C # programmers more beautiful and productive, hope C # is better and better!

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.