C #6.0 new syntax features (2)

Source: Internet
Author: User

Previously, I introduced some syntax FEATURES OF C #6.0 in the new syntax of C #6.0 through Roslyn. Now with the release of Visual Studio 14 ctp3, we can try some new features one after another. Here we will briefly introduce the new syntax that we have not introduced before.

Property expressions)

We often write read-only attributes generated by functions in the class:

???? Classpoint
???? {
???????? Publicint X {Get; set ;}
???????? Publicint y {Get; set ;}

???????? Public point (int x, int y)
???????? {
???????????? This. x = X;
???????????? This. Y = y;
????????}

???????? Publicdouble distance
???????? {
???????????? Get {returnmath. SQRT (x * x) + (y * y ));}
????????}

???????? Publicpoint move (INT dx, int Dy)
???????? {
???????????? Returnnewpoint (x + dx, Y + dy );
????????}
????}

Now, you can use a Lambda expression to simplify this process:

???????? Publicdouble distance => math. SQRT (x * x) + (y * y ));

?

Function expression (method expressions)

Function expressions are similar to attribute expressions, so that we can use lambda expressions to simplify member functions. Take the above point as an example. The move function can be simplified as follows:

???????? Publicpoint move (INT dx, int Dy) => newpoint (x + dx, Y + dy );

Finally, we will summarize several new features introduced in the previous article to simplify the Point class:

???? Classpoint (int x, int y)
???? {
???????? Publicint X {Get; set;} = X;
???????? Publicint y {Get; Set ;}= y;

???????? Publicdouble distance => math. SQRT (x * x) + (y * y ));

???????? Publicpoint move (INT dx, int Dy) => newpoint (x + dx, Y + dy );
????}

?

?

Null Check operator (monadic null checking)

This is a syntax that I like very much. For example, if we want to obtain the X coordinate of the first vertex of a point sequence, we will write it like this:

???? Int firstx = points. First (). X;

However, laruence will tell you that there is no null check. The correct version is as follows:

???? Int? Firstx = NULL;
???? If (points! = NULL)
???? {
???????? VaR first = points. firstordefault ();
???????? If (first! = NULL)
???????????? Firstx = first. X;
????}

It is correct, and the code extraction is much harder to read. Now, in C #6.0, ?. The preceding code can be changed to the following format:

???? Int? Firstx = points ?. Firstordefault ()?. X;

In this example, we can also see its basic usage: if the object is null, null is directly returned without the subsequent operation of obtaining members.

Note that because "?. "The operator can return NULL. When the returned member type is struct ,"?. The Return Value Type of the "." and "." operators is different.

???? Point P = newpoint (3, 2 );

???? Console. writeline (p. x. GetType () = typeof (INT ));???????? // True
???? Console. writeline (P ?. X. GetType () = typeof (Int ?));???????? // True

Besides "?. "Is there another "? [] "Operator so that we can write the following expression:

???? Int? First = customers? [0]. Orders. Count ();

?

Nameof expression (nameof expressions)

We often use Attribute names in the form of strings in reflection or similar techniques, regardless of spelling errors. When we refactor and modify the attribute name, because the compiler does not check the properties of the string type, modifying the corresponding string attribute name is a very headache. Now with nameof, you no longer have to worry about spelling the attribute name.

The nameof operator can act on variables, functions, classes, and namespaces and return their names. For example:

???? Staticvoid main (string [] ARGs)
???? {
???????? Console. writeline (nameof (main ));???????? // Output "Main"
????}

When the parameters are concatenated by the "." operator, only the last name is returned, for example:

???? Console. writeline (nameof (leleapplication1.program ));???????? // Output "program"

This is also understandable, because leleapplication1.program and program are equivalent.

Note that, because C # allows function overloading, functions with the same name exist. For example:

???? Staticvoid Foo (){}
???? Staticvoid Foo (int x ){}

The following two problems exist:

  1. To which function should I jump?
  2. When renaming a function and maintaining the original name of another function, do I need to change the nameof field?

These two problems are only reflected in Visual Studio. They are not syntax ambiguity and do not affect the running results. There is also a special article on codeplex to discuss it. The current processing method is as follows:

  1. To which function should I jump? ???? (Who first defines who to go)
  2. When renaming a function and maintaining the original name of another function, do I need to change the nameof field? (The rename function does not rename the nameof. renaming other types, such as attributes, will change together)

?

Await is supported in catch and finally statement blocks.

The await operator is introduced in C #5.0 to facilitate asynchronous operations. At that time, it could not be used in catch and finally. It was a bit confusing. This restriction was opened in C #6.0, making it easier to use.

???? Try
???? {
???????? Res = await resource. openasync (...);
????}
???? Catch (resourceexception E)
???? {
???????? Await resource. logasync (Res, e );???? // Now supports
????}
???? Finally
???? {
???????? If (res! = NULL) await res. closeasync (); // now supports
????}

?

Catch supports filtering conditions.

The filtering conditions supported by catch are also one of the most popular features. Now, you can try again.

???? Try
???? {
???????? Foo ();
????}
???? Catch (exception e) if (E. hresult = 0x800000c)
???? {
???????? // Do something
????}

?

Other unsupported features

I will only introduce the new features that can be used currently. I have tried to add so many new features. Other features that have not yet been released will be introduced in the next updated version. Interested friends can look at the official feature Implementation Status documentation: http://roslyn.codeplex.com/wikipage? Title = language % 20 feature % 20 Status & referringtitle = home. Currently, we are looking forward to the new features of string interpolation and pattern matching, especially pattern matching. We hope to have an early experience.

In addition, we have not found any update on the BCl. Although the Bcl has been well-developed, I feel that this update has a very large granularity. It is estimated that there will be at least some basic library supplements.

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.