Future Plans for C#

At NDC London Mads Torgersen proposed future language changes to C#. It should be noted that these are just proposals and are not guaranteed to make it into any specific release. Damien Guard has posted a summary and brief analysis of these proposals, but for your convenience here are some of the highlights.

Read-Only Properties

Read-only auto properties will allow developers to declare properties and their backing store in a single line.

public int X { get; } = x;

Static Type Using Statements

Visual Basic and Java already allow developers to import modules (C# static class) into the namespace. This allows removing repetitive code such as “Math.” in front of commonly used static functions.

Primary Constructors

By placing parameters after the class name, developers will not need to explicitly create a constructor. This removes the tedious code for copying the constructor parameters to private fields.

public class Point(int x, int y) {

private int x, y;

}

Property and Method Expressions

Property expressions would eliminate some of the boilerplate needed for simple read-only properties.

public double Distance => Math.Sqrt((X * X) + (Y * Y));

Method properties would do the same, expect of course they accept parameters.

Note that parameterized properties are still not under consideration. That remains a VB-only feature for the foreseeable future.

Function Parameters

These days most developers never use arrays expect when needed for the params keyword. So one proposal is to also support the IEnumerable<T> interface with params. If this is done, other languages such as Visual Basic would also need to support it.

Another proposal is to allow local variables to be declared using the out keyword. For example,

int.TryParse("123", out int x);

Null Propagation

When working with messy data, developers often have to write a series of null checks before reading a property or invoking a method. The ?. syntax would eliminate that by conditionally invoking a method if the preceding value is not null.

var bestValue = points?.FirstOrDefault()?.X;

In this case if points is null, or points.FirstOrDefault() returns a null, then the .X is ignored a null is returned instead. This can then be chained with a ?? to supply an alternate default value.

var bestValue = points?.FirstOrDefault()?.X ?? -1;

This sematic is found in “message passing” languages such as Objective-C and Smalltalk. It is commonly cited as being problematic because what would have been a null reference exception is instead silently ignored.