Feature picks from C# 6

These are my feature picks from the upcoming C# 6:

New nameof operator

The nameof operator provides the name of the target variable. We can use it to prevent debugging messages becoming inconsistent after refactoring variable names.

if (!_Validate(name)) {
    throw new ArgumentException(nameof name + " is not valid");
    // throws "name is not valid"
}

Primary constructors

Primary constructors allow us to initialise fields and properties without a constructor.

Instead of having to perform an assignment in the constructor like this:

public class Cache
{
    private int _capacity;
    
    public Cache(int capacity)
    {
        _capacity = capacity;
    }
}

We can now use the shorthand:

public class Cache(int capacity)
{
    private int _capacity = capacity;
}

If you want to perform logic in the constructor, then you can call this():

public class Cache(int capacity)
{
    private int _capacity = capacity;
    
    public Cache(int capacity) : this(capacity)
    {
        m_oInnerCache = StaticCache.getInstance();
    }
}

However, in most cases this is not recommended.

Auto-property initialisers

You can initialise properties inline, instead of having to use the constructor:

public class BoardGame
{ 
    public int TurnLimit { get; } = 10;
}

This reduces the need for a constructor, especially when coupled with primary constructors.

Declaration expressions

You can declare an out parameter in an expression so that it is scoped tighter.

if (decimal.TryParse(test, out var amount))
{
    return amount;
}

Amount is not accessible outside the scope of the if-statement.

Further reading

Scott Allen has longer descriptions of each feature on his blog Ode to Code.

csharp 

See also