BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News C# 8 Small Features

C# 8 Small Features

Leia em Português

This item in japanese

While most of the attention is on big ticket items such as default interface methods and nullable references, many small features are also be considered for C# 8. Here is a sampling of things that may make it into future version of C#.

New Assignment Operators: &&= and ||=

Since the first version, C# has supported a syntax that combines assignment with another operator. This includes pretty much all of the binary operators (e.g. +, -, &, etc.) expect the short-circuiting Boolean operators && and ||. The "&&= and ||= assignment operators" proposal would complete this list.

Verbatim Interpolated Strings

Verbatim strings are started with a @". Interpolated strings use $". But what if you want to have a string that is both verbatim and interpolated? Is that @$" or $@"? Currently one of those works and the other is a compiler error, but which is which is often hard for people to remember.

In a modest proposal called verbatim interpolated string, the syntax will be extended to accept @$". This will be interpreted exactly the same as interpolated verbatim strings ($@"), so you’ll never again have to worry about getting it backwards.

There is some minor controversy regarding this change, as some think it isn’t necessary or would lead to inconsistencies.

Allow `using` statement to structurally match `IDisposable`

Interfaces have strange relationship with the C# compiler. Quite often you don’t have to actually implement a specific abstract interface to use a language feature; you merely have to create a public API on the class that resembles the abstract interface.

The classic example is `foreach` and IEnumerable. If the class has a method called GetEnumerator and that returns an object with a Current property and MoveNext method, then you can use foreach. The actual return types don’t matter, which allows classes such as List<T> to implement faster enumerators. This is often called “structural matching”.

Under this proposal, the `using` statement will also support structural matching. At first this doesn’t seem to be useful, as you wouldn’t expect to see a disposable class that doesn’t implement IDisposable. However, there is a new kind of type called a ref struct. These cannot implement interfaces, so they couldn’t be used with the using statement without this proposal.

Extension methods with foreach and using

A follow-up to the previous proposal is the ability to add GetEnumerator or Dispose as an extension method and have it work with `foreach` or `using` respectively. Again, we’re talking about a feature that would only be used in very specialized circumstances. For example, you could add a Dispose extension method to a COM object from a third-party library (which may be a good place to call Marshal.ReleaseComObject).

This is not a formal proposal yet and may be incorporated into the aforementioned changes to the `using` statement.

Implicitly Scoped `using` Statements

Currently a `using` statement can only be followed by an explicit scope (i.e. a pair of braces) or another `using` statement. If this proposal is accepted, then you can write this instead:

using var a = new MyDisposable();
using var b = new MyDisposable();
using var c = new MyDisposable();

Each of these variables will automatically be disposed at the end of the current scope, in reverse order. Functionally this is equivalent to this, but is less verbose and doesn’t intro a new scope.

using (var a = new MyDisposable())
using (var b = new MyDisposable())
using (var c = new MyDisposable())
{
    // Some code
}

This change could be especially useful when multiple disposable objects are needed, but are not created all at the same time. You could even create disposable objects in the middle of an expression with confidence that it will be disposed at the end of the current scope.

var results = myExcelReader.ReadResults(using new MyExcelSheet(excelFilePath));

One criticism of this proposal is that it isn’t compatible with some statements such as `goto`.

Rate this Article

Adoption
Style

BT