C # 10 Common features

Source: Internet
Author: User

1) async/await

Using the async/await mode, you can not block the UI or the current thread while executing a code block operation. Even if the operation is delayed by certain execution actions (such as a Web request), the async/await pattern will continue to execute subsequent code.

Microsoft Documentation: https://msdn.microsoft.com/zh-cn/library/hh191443.aspx

2) object/array/collection initializer (initializers)

You can easily create instances of classes, arrays, and collections by using initializers for objects, arrays, and collections:

Sample class

public class Employee {

public string Name {get; set;}

Public DateTime StartDate {get; set;}

}

Create an employee instance with an initializer

Employee EMP = new Employee {name= "John Smith", Startdate=datetime.now ()};

The code in the example above can be very helpful in unit testing, but in some cases it should be avoided, such as when the class should be instantiated by a constructor.

Microsoft Documentation: https://msdn.microsoft.com/zh-cn/library/bb384062.aspx

3) LAMBDA expression, predicate delegate (predicates), Delegate (delegates), and closure (closures) 4)?? –null merge operator (null coalescing operator)

When the left-hand side of the expression is not null,?? operator returns the value to the left of it, otherwise the value to the right of it is returned:

May be null

var somevalue = service. GetValue ();

var defaultvalue = 23

If somevalue is null, result is 23

var result = Somevalue?? DefaultValue;

?? An operator can be used for chained operations:

String anybody = Parm1?? Localdefault?? Globaldefault;

It can also convert nullable types to non-nullable types:

var totalpurchased = purchasequantities.sum (kvp = kvp. Value?? 0);

Document: Https://msdn.microsoft.com/zh-cn/library/ms173224.aspx

5) $ "{x}" – Interpolated string (string interpolation)-C # 6

A new feature of C # 6 that allows string concatenation in a more efficient and elegant way:

Traditional way

var somestring = String.Format ("Some data: {0}, Some more data: {1}", somevariable, someothervariable);

The New Way

var somestring = $ "Some data: {somevariable}, Some more data: {someothervariable}";

You can also write C # expressions in curly braces, which makes it very powerful.

6)?. –null conditional operator (null-conditional operator) –c# 6

The null condition operator is used as follows:

If customer or customer.profile or customer.profile.age is null, the result is null

var currentage = customer?. Profile?. Age

It's not going to happen again, Nullreferenceexceptions!

Document: Https://msdn.microsoft.com/zh-cn/library/dn986595.aspx

7) nameof Expression –c# 6

The new nameof expression may seem less important, but it does have its niche. When using an automatic refactoring tool (such as Resharper), you may need to represent it by the name of the parameter:

public void Printusername (User currentUser)

{

When renaming CurrentUser, the refactoring tool may omit this variable name in the text

if (CurrentUser = = null)

_logger. Error ("Argument CurrentUser is not provided");

//...

}

Now you can write it like this:

public void Printusername (User currentUser)

{

Refactoring tools Don't miss this.

if (CurrentUser = = null)

_logger. Error ($ "Argument {nameof (CurrentUser)} is not provided");

//...

}

Document: Https://msdn.microsoft.com/zh-cn/library/dn986596.aspx

8) Property initializer –c# 6

You can specify an initial value when declaring a property by using the property initializer:

public class User

{

Public Guid Id {get;} = Guid.NewGuid ();

// ...

}

Using the property initial

One benefit of using a property initializer is that you do not have to declare a setter method, which makes the property immutable (immutable). Property initializers can be used in conjunction with the main constructor (Primary Constructor) syntax of C # 6. (Translator Note: Primary Constructor syntax allows you to define a class at the same time, immediately after the class name to specify a constructor with parameters)

9) as/is operator

The IS operator is used to determine whether an instance is of a specific type, such as whether you want to see if the type conversion is feasible:

if (person is adult)

{

Do stuff

}

The as operator attempts to convert an object to an instance of a particular class. If it cannot be converted, NULL is returned:

SomeType y = x as SomeType;

if (Y! = null)

{

Do stuff

}

) yield keyword

You can return data items for the IEnumerable interface by using the yield keyword. The following example returns 2 of the sub-Parties (for example, until 8 times: 2, 4, 8, 16, 32, 64, 128, 256):

public static ienumerable<int> Power (int number, int exponent)

{

int result = 1;

for (int i = 0; i < exponent; i++)

{

result = result * number;

yield return result;

}

}

If used properly, yield can become very powerful. It allows you to delay the generation of objects in the sequence, such as when the system does not need to enumerate the entire collection, and can be stopped on demand.

C # 10 Common features

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.