Roslyn and the future of .NET languages – Language Features

Boston Code Camp 22 materials: Slides | Language Features Demo Code | Post on Extensions

The new Visual Studio 2015 Roslyn language features include a grab bag of nice changes to help make code more compact and readable. This isn’t an exhaustive list but some of my favorites.

Getter only and initialized auto-properties

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

This is a much simplified way to create readonly properties, which previously required a full property with a backing field declared as readonly. This is not the same as an auto-prop with a private setter, which is still a mutable value. The value can be set from a constructor or in line since auto-properties can also now be initialized directly.

Null conditional operator ?.

NameSize = Name?.Length.ToString() ?? "0";

This is going to be great for cutting down on surprise NullReferenceExceptions and is so much more compact than a string of if statement checks.

Interpolated Strings

Console.WriteLine($"User name is {Name}");

Finally, the functionality of String.Format has some readable syntax! In the current VS Preview the syntax uses "\{Expression}" but this will change to use the $ syntax before release.

Expression bodied members (C# only)

public int CountMultiple(int multiplier) => Count * multiplier;

This is mostly just cutting down on space required for these declarations.

await in catch and finally blocks (C# only)

try
{
    await operation.DoWork();
}
catch
{
    await operation.Rollback();
}
finally
{
    await operation.Close();
}

One of those seemingly simple things that could stop you in your tracks if you were in a situation that needed it in C# 5 and needed a lot of extra code to work around.

Read-write properties to implement Read-only interface properties (new in VB)

Public Interface IHaveData
    ReadOnly Property Name As String
End Interface

Public Class ClassDemo
    Implements IHaveData
    Public Property Name
        Get
            Return ""
        End Get
        Set(value)

        End Set
    End Property
End Class

Another one that’s so simple but could really get in the way when it wasn’t available.

 

Download the Visual Studio 2015 Preview: http://www.visualstudio.com/en-us/downloads/visual-studio-2015-downloads-vs

Check out the full list of new language features and latest updates (and the source code!) on CodePlex: http://roslyn.codeplex.com/wikipage?title=Language%20feature%20status&referringTitle=Home

One thought on “Roslyn and the future of .NET languages – Language Features

Leave a Reply