Quick rundown of possible new C# 6.0 features (perhaps, maybe - I think)

January 19, 2015 by C#  

While doing my research for this post, I ran into quite a few sources containing features that doesn't actually seem to exist in C# 6.0. This is thanks to early released previews to the public that contained features that the guys at Microsoft decided to rather exclude or didn't include for preview yet.

The following post contains a list of features added to C# 6.0, based on the VS2015 preview, not sure how close we are to the official release, soooo this might, perhaps, maybe, possibly, conceivably, for all we know be some of the new C# features...

Auto-property initializers

It is now possible to assign values to auto-properties like so.

public string FirstName { get; set; } = "John";

Getter only auto-properties

We can now create readonly properties in this manner (also assignable in its class constructor).

public string FirstName { get; } = "John";

Dictionary initializer

Not sure what the reason(s) are behind this syntax addition, but you can now alternatively initialize a dictionary like seen below as well.

Dictionary<string, string> LOTR = new Dictionary<string, string>()
    ["Gandalf"] = "Wizard",
    ["Legolas"] = "Sindarin Elf",
    ["Frodo"] = "Constipated Hobbit"

Null-conditional operators

I really like the addition of the following syntax, its a much sexier way to do our null checks.

int? numberOfProducts = products?[1].Colours?.Count();

using static

It is now possible to import the static members of a class directly into our scope, not awfully excited about this feature at the moment, but it might grow on me with time.

using System.Console;

class Program
    static void Main(string[] args)
        WriteLine("What sorcery is this?");

Parameterless struct constructor

We're now no longer forced to supply parameters for structs - I am sure I don't need to supply a code snippet for this one ;)

Exception filters

Not a life changing change, but you can now add an if statement next to your catch statement, nice thing however, is that when a condition is not met, it will evaluate the next condition(s), hence no rethrowing of exceptions needed.

    throw new Exception("Dude");
catch (Exception ex) if (ex.Message == "Where is my car?")

Expression-bodied members

Another not so life changing addition, is that we can now use expressions as our member bodies as an alternative to statement blocks (and the crowd goes mild - yay).

public double MyAge() => (DateTime.Now - new DateTime(1983, 2, 2)).TotalDays / 365;

Await in catch/finally

Its now possible to use the await statement in a catch/finally block.

String interpolation

This is new functionality I like quite a bit, think of it as String.Format that took the next step of its evolution, currently it looks something like this:

string s = "\{p.FirstName} \{p.LastName}";

But apparently this will be a bit cleaner in the final release and look something like this:

string s = $"{p.FirstName} {p.LastName}";

nameof keyword

The last new feature I am going to show you, before leaving you to fend for yourself in the wild, is the nameof keyword. In the spirit of refactoring this is quite a nifty little feature, it allows you to display the name of a program element e.g.

Console.WriteLine(nameof(p)); // prints p

Feeling a bit empty and unfulfilled? You can read more about the upcoming c# 6.0 release over here.

Leave a Comment

Related Posts

New Features - C# 4.0

January 12, 2010