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?

 

Csharp6

 

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.

Properties

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;}};

Initialization

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:

indexObject["x"];

you can write

indexObject.$x

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){
this.x=x;
this.y=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:

"Hello<newLine>World";

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

Exceptions

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

try{
 do something
}
Catch {
 await cleanup();
}
Finally{
 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. 

 

 Csharp6

Banner


Is PHP in Trouble?
10/04/2024

The April 2024 headline for the TIOBE Index, which ranks programming languages in terms of their popularity, reads, "Is PHP losing its mojo" asking this question because this month PHP has dropped out [ ... ]



Conference Times Ahead
29/03/2024

Following a well-established pattern both Google's and Microsoft's Developer Conferences will take place in May while Apple follows on in June. Here are the dates plus what to expect.


More News

 

raspberry pi books

 

Comments




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

 

 

 

 

 

 

 

 

Last Updated ( Wednesday, 09 April 2014 )