Future of C#

Earlier this month, I moved to Wellington – wet, but nice city – and to get the best feel of it as a developer, I decided to join Wellington .NET User Group.

The first meet up was with Ivan Towlson – brilliant .NET developer from UK – who gave an excellent talk about the future of C#. It was focused on all the big news brought from the BUILD conference. So, what Ivan spoke about?

First of all, every new version of C# brought at least one major feature. C# 2.0 was all about generics, version 3.0 introduced LINQ, 4.0 added dynamic language features and 5.0 helps with asynchronous programming. In comparison to that, version 6.0 is more about tooling and Roslyn.

If you haven’t heart it yet, Roslyn – the C# compiler – has been open-sourced. It’s a massive step for the whole .NET ecosystem and for Microsoft as well. Thumbs up!

Apart from that, there is a few minor improvements in the language. Let’s take a look at them.

Simplified Class Implementation

First thing on the list is the implementation of default constructor.

public sealed class Foo(int id, private readonly int downloadSize)
    public int Id { get; } = id;

    public int Method(int transferRate)
        return donwloadSize / transferRate;

The definition is removed from the class body to the class definition and you can use the fields in methods and properties. It’s a little weird to have the constructor definition at the top, but let’s give this feature its time. If you chose to implement the constructor like this, you can’t modify its body to for example perform the validation of arguments.

Also, notice the property definitions – simplified syntax of properties with backing field. Finally! This is the feature that removes the pain of writing too much boring boiler-plate code.

Convenience Features

Next features are about making your coding life a bit easier as well. If you add a static class to your using statements, you can omit it when calling its method as if you would use a local one.

using System.Math;

x = -2;
positiveX = Abs(x); // instead of Math.Abs

After that, there is a new way of initializing your dictionaries, which is a bit more readable than the previous one.

var values = new Dictionary<int, string>
    [0] = "value",
    [10] = "another"

And you can use variable declarations – they are particularly handy when working with out parameters.

Decimal.TryParse(text, out decimal amount);
return amount;

var square = (var x = r.Next(...)) * x; 

Planned features

Apart from already implemented features, there is a few of drafts and plans.

One of the most controversial changes is a null checking operator. So, instead of the typical null checking code you would use something similar to the following code.

int? firsNameLength = customer?.Name?.First?.Length
custom?.Call() // if null, method won't be call

I like reducing the code noise, but it might lead to issues with less experienced developers who would just move the problem of NullReferenceException a bit further.

Other feature for enhancing expressiveness might be an in-line lambdas methods.

public int Method() => downloadSize / transferRate

Another small change might be with event initializers to make them work in a manner the property initializers do.

var foo = new Foo
    ButtonClicked = (o) => { ... },

There are discussions about changing API of current classes to accept IEnumerable interface instead of arrays. LINQ would become even more easier to use and the need for calling .ToArray() method would promptly disappear.

To make interoperability with dynamic object easier, dictionary could work a bit like a json objects.

dictionary.$key = "value";

There might be some changes with exceptions. One interesting is an await block inside a catch statement. Another one is conditional exception handling, which seems really nice.

catch (Exception ex) if (ex ...)

And the last one that was mentioned is type inference in constructors.

// so this
var x = new Ctor<int, int>(10, 10)

// could become just
var x = new Ctor(10, 10)

I like most of the new features and even the planned one and I’m really happy that Microsoft open-sourced Roslyn. Good work guys!

Would you like to get the most interesting content about C# every Monday?
Sign up to C# Digest and stay up to date!