BT

Future Plans for C#

by Jonathan Allen on Dec 14, 2013 |

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.

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Except for null propagation, by Yaakov Davis

there's little chance for the rest to make it to the spec, IMO.

Auto properties by Jakob Ooms

public int X { get; } = x
How ugly...
Why not

public int X {get; private readonly set;}
so that the setter can only be invoked from the constructor.

Re: Auto properties by Jonathan Allen

Yea, I like that one better. In my opinion C# has a bad habit of making us repeat ourselves.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

3 Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT