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.


One 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:

public int Y { get; } = y;

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:

int X=>x*x;

defines a member which returns the square of x i.e.

int X {get {x*x;}};


The team have generally been busy implementing other variations on initializers. For example you can now initialize a Dictionary object

new Dictionary<string><int>{ ["x"]=3, ["y"]=7 };

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.

public class Point(int x, int y){}

then private members are created for the parameters and automatically assigned and the example is equivalent to:

public class Point{
private int x,y;
public Point(int x, int y){

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:

new Button {OnClick+=eventHandler};

this almost feels like JavaScript!

Expressions And Literals

Talking 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:

var x=(var y=rand(10);y*y);

sets x to the square of a random number. 

Another new expression feature is the ability to declare variables within an expression. For example

var y= (int x =3) * x;

sets y to 9.  

There are some small but nice additions to the way literals can be used. There is now a binary literal:

int x=0b0111;

You can use digit separators for presentation and to make sure you counted the correct number of digits:

int x = 0xFF_00_FF_00;

You can easily create multiline string literals:


and string literals can incorporate variables using string interpolation:

"Hello \{record.name) how are you to day"

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:

var a= Math.Sqrt(Math.Sin(0.5));

but now you can write

Using System.Math;
var a= Sqrt(Sin(0.5));

Also see: C# Gets A New Operator - Safe Navigation


There are also some additions to exception handling. You can now use an await in a catch and finally statement. For example:

 do something
Catch {
 await cleanup();
 await cleanup();

Another nice extra for exception handling is the ability to use filters. For example

catch(exception e) if (e.count>5) {
 do something

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. 




Google Gemini API Developer Competition

Google is running a Gemini API Developer Competition with prizes including a 1981 custom electric DeLorean. Entrants will use the Gemini API to tackle real-world challenges, and the organizers su [ ... ]

Pure Virtual C++ 2024 Sessions Announced

Microsoft has announced the sessions for Pure Virtual C++ 2024, which is taking place on April 30th 15:00 UTC. People who sign up will get access to five sessions happening on the day, alongside a ran [ ... ]

More News


raspberry pi books



or email your comment to: comments@i-programmer.info









Last Updated ( Wednesday, 09 April 2014 )