Greg Reddick is a noted speaker, author, and software engineer. This blog covers all aspects of programming, particularly for Windows, and other related topics.

2017-03-18

C# Version 7 New Features

Visual Studio 2017 shipped earlier this month. Visual Studio itself has new features, but this article is concentrating on the C# programming language. I made a previous post on what they were talking about early in the development of C# version 7, but now we get to see what actually made it into the final product.

While these features are in the shipping version, there is yet to be a formal specification of the language for C# version 7. The last formal specification was for C# version 5. It would be good if they could get the spec updated.

The order of interpreting case statements has changed. Before it was undefined the order they were executed. Now, because order matters as the two Rectangle cases above show, case statements are executed in order, except the default is always executed last, regardless of order.

There is more stuff that matching can do.

3) Tuples. I think this is the one that might affect my code the most as it gives the ability to return more than one value from a method call. The Lua programming language has a way of doing that, and I found it cool. Tuples is actually more powerful than what Lua can do. Example of a declaration:

The underscores are ignored, but may improve readability of some numbers.

6) Ref returns and locals. Methods can return information by reference instead of by valid. See the example in the link above. This is particularly useful in returning pointers into a large data structure. For a few things this will vastly speed up code.

7) Async can now return things that are not Task. Creating things using these is not going to happen a lot, but calling things defined with them probably will.

8) You can now use expression bodies (=> definitions) in accessors, constructors, and finalizers. Some people love these...I just find them a weird syntactic sugar.

9) Throw from the middle of an expression. Okay fine. There might be a day where I find this useful...but it doesn't seem likely right now.