C# 6.0 Features |
Written by Mike James |
Tuesday, 08 April 2014 |
With the release of Roslyn, the .NET cloud compiler, we have our first view of the features that are likely to make it into C# 6.0 - why so little publicity over a major event?
Roslyn is the completely reworked .NET compiler and one of its promised features is that it makes it easier to add language features. As part of the release of the Roslyn source code to the .NET Foundation we also have a list of "language features". These list the features, all 35 of them, that have been or are being added to C# and to Visual Basic. Many of these new features are small modifications to things that already exist and the notes state that they are far from final and could be changed at any time. This is all the more likely given that Roslyn clearly does make language changes easier. So what are the big new features? To be honest - there aren't any. Most of the changes that are listed are small and often just amount to what has come to be called syntactic sugar i.e. shorter ways of writing things that you can already do. PropertiesOne nice change is to the way we can create auto-properties. You can now define read only auto-properties and you can initialize them. For example:
creates a "get-only" property Y and initializes it to y. What is slightly stranger is the idea of an expression bodied member a mix of property initialization and lambda expressions. For example:
defines a member which returns the square of x i.e.
InitializationThe team have generally been busy implementing other variations on initializers. For example you can now initialize a Dictionary object
and any object supporting a Indexer as a property (see: In search of default properties). You can also perform an index access using a property syntax. For example instead of:
you can write
The new primary constructor provides another way to initialize objects. If you include something that looks like a constructor as part of the class definition, e.g.
then private members are created for the parameters and automatically assigned and the example is equivalent to: public class Point{ That is the primary constructor defines a set of private members that are initialized when the object is created. Once you have a primary constructor all other constructors have to call it using the form this(primary parameters) e.g. this(x,y) so that it can initialize the private members. A more surprising new initializer is for an event:
this almost feels like JavaScript! Expressions And LiteralsTalking of JavaScript (or C) is also a new semicolon operator which lets you string together as many expressions as you like and the value of the expression is the value of the last one. The purpose of this addition is to let you have multiple steps in parts of the language that only allow a single expression to be entered. For example:
sets x to the square of a random number. Another new expression feature is the ability to declare variables within an expression. For example
sets y to 9. There are some small but nice additions to the way literals can be used. There is now a binary literal:
You can use digit separators for presentation and to make sure you counted the correct number of digits:
You can easily create multiline string literals:
and string literals can incorporate variables using string interpolation:
although this one is only a "maybe" so it might not make it into C#6. Another nice extension is the ability to use static methods more easily. For example if you just want the square root and use some other math functions you currently have to write things like:
but now you can write
Also see: C# Gets A New Operator - Safe Navigation ExceptionsThere are also some additions to exception handling. You can now use an await in a catch and finally statement. For example:
Another nice extra for exception handling is the ability to use filters. For example
If the condition is true then the catch is entered otherwise the exception keeps going. This is better than handling the exception, doing the test in the catch block and then rethrowing the exception because it keeps the stack intact. You can find a full list of the new features complete with their current status at the Rosyln site.
So what do you think of C#6? It really does look like a lot of syntactic fiddling but mostly nice. Is this what the future holds for C#? Does it really miss nothing big that would make it a better language? I think that C# has reach a level of maturity where it really has settled down and its features probably could not be frozen. This of course itn't true of its associated class library which is where most of the utility in modern languages lives.
More InformationLanguage feature implementation status Related ArticlesC# Gets A New Operator - Safe Navigation RyuJIT - The Next Gen JIT .NET Compiler Microsoft tells C# developers to learn JavaScript!? Microsoft Team Explains Language Stagnation
To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin, or sign up for our weekly newsletter.
Comments
or email your comment to: comments@i-programmer.info
|
Last Updated ( Wednesday, 09 April 2014 ) |