Syntax that may appear in C # 7.0

Source: Internet
Author: User

The main features of Microsoft's work List on Microsoft's C # 7.0 feature, as seen today on MSDN, are:

tuple enhancement

The readability of a tuple is always not very good, and many times it would be better to write a new class without using ITEM1, Item2 this kind of unknown property:

???? publicTuple<int, int> Tally (IEnumerable<int> Values) {...}
???? var t = Tally (myvalues);
???? Console. WriteLine ($ "Sum: {t.item1}, Count: {t.item2}");

In C # 7.0, you can write this way:

????Public(IntSumIntCount) Tally (IEnumerable<Int> Values)
???? {
????????Var s = 0; var c = 0;
???????? foreach (var value in Values???????? return (s, c); //target typed to (int sum, int count)
????}

???? (var sum, var count) = Tally (myvalues); //deconstruct result
???? console

This is basically the way F # is written, and the readability is much better.

?

Pattern matching

Pattern matching was first seen in the syntax plan for C # 6.0 and is now seen again in C # 7.0:

???? Expr simplify (expr e)
???? {
????????Switch(e)
???????? {
????????????CaseMult (Const (0), *):ReturnConst (0);
????????????CaseMult (*, Const (0)):ReturnConst (0);
????????????CaseMult (Const (1), var x):ReturnSimplify (x);
????????????CaseMult (var x, Const (1)):ReturnSimplify (x);
????????????CaseMult (const (var l), Const (VAR r)):ReturnConst (L * r);
????????????case Add (Const (0), var x): return simplify (x);
???????????? case Add (var x, Const (0)): return simplify (x);
???????????? case Add (const (var l), Const (VAR r)): Return Const (L + R);
???????????? case Neg (Const (var k)): return Const (-K);
???????????? default: return E;
????????}
????}

Looks pretty good, for the need of occasions can greatly reduce the code, feeling pattern matching and tuples to use, it is estimated that there will be a piece of it.

?

Non-null reference

This is also very early to see someone put forward, if it can be formally enabled, the words are very good, the code inside those places to add null judgment is very clear, and will not be able to see the UT null judgment.

???? dog! Mandatorydog = new Dog ("Mandatory");
???? Mandatorydog.bark (); //Ok-can call method on mandatory reference.
???? string name = Mandatorydog.name; //Ok-can Access property on mandatory reference.

Enhancements for asynchronous support

This is also a relatively large change, the using, yield and other syntactic sugar can be added to asynchronous support, and introduce iasyncdisposable, and so on, processing asynchronous iterations is very convenient, because there is no suitable example, here is not listed.

?

Other

There are also many other very useful grammar, interested friends can go to https://github.com/dotnet/roslyn/issues/2136 here to see. However, it is almost certain that C # 7.0 will not support so many grammars, but hopefully it will support those key features.

Syntax that may appear in C # 7.0

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.