Mads Torgersen, C# program manager at Microsoft, published a short video presentation describing what is coming in the next major C# version, C# 6. Among C# 6 new features, Mads highlighted getter-only properties, the lambda-arrow operator, string interpolation, and more.

First off, says Mads, C# 6 will not change C# design philosophy and will mostly provide many small features that will help clean up code.

Getter-only auto-properties

C# 6 will allow the definition of immutable auto-properties, i.e., auto-properties that will only support a getter method:

public class Point { public int X { get; } public int Y { get; } public int ReadWrite { get; set; } }

Getter-only auto-properties are backed by a read-only field that can be assigned in the constructor.

String manipulation

The traditional C# syntax for string interpolation is described by Mads as "kind of confusing and error-prone":

return String.Format("({0}, {1})", X, Y);

and will be superseded by a new string interpolation syntax:

return "(\{X}, \{Y})";

Lamba-arrow operator for single-methods expressions

The new lambda-arrow operator will simplify the definition of methods that just return the value of a single expression:

public override string ToString() => "(\{X}, \{Y})"; public double Dist => Sqrt(X * X + Y * Y);

This will help reduce a lot of boilerplate code typing, according to Mads.

Index initializers

In current C#, index setters have to be assigned in separate statements:

var result = new JObject(); result["x"] = X; result["y"] = Y;

In C# 6, it will possible to initialize the object with one expression:

var result = new JObject() { ["x"] = X, ["y"] = Y };

which leads to a highly clean and readable definition of a ToJSon method:

public JObject ToJson() => new JObject() { ["x"] = X, ["y"] = Y };

Null-propagating operators

Null-conditional operator is a new feature aiming at making checks for null values kind of "fade into the background". So, instead of having this kind of checks:

if (json != null && json["x"] != null && json["x"].Type == JTokenType.Integer)

C# 6 will allow to use a new ? operator that will calculate the part of the expression following it only if it is applied to a non-null object. So the if statement above will shrink to:

if (json?["x"]?.Type == JTokenType.Integer)

Exception filtering

Exception filtering allows to catch expression only if a particular condition is verified:

try { } catch (ConfigurationException e) if (e.IsSevere) { }

which is preferable to the catch-rethrow idiom, says Mads, because, when rethrowing, information about where the exception originally arose is lost.

Await in catch and finally blocks

Finally, it is now possible to use await in catch and finally blocks, which adds a great deal to flexibility in error handling. This was kept out of previous C# versions, Mads remarks, only due to the fact that it was not clear yet how that feature could be implemented.

try { } catch (ConfigurationException e) { await LogAsync(); } finally { await CloseAync(); }

The official resource to get more information about C# includes more detailed information and a complete feature list.

InfoQ has already covered in the past C# 6 new features, such as pattern matching, easier immutable objects.