InfoQ Homepage News C# 8 Small Features

# C# 8 Small Features

Leia em portuguÃªs

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.

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

Style

## 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.

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

• ##### &&= and ||=

by Dan Sutton /

• ##### Re: &&= and ||=

by Andrew Witte /

• ##### Re: &&= and ||=

by Cameron Purdy /

• ##### Re: &&= and ||=

by Steve Russell /

• ##### Re: &&= and ||=

by Cameron Purdy /

• ##### Re: &&= and ||=

by Steve Russell /

• ##### &&= and ||=

by Dan Sutton /

Your message is awaiting moderation. Thank you for participating in the discussion.

Hmmm... I'm not sure what these would do. &= and |= already exist; those are Boolean operators doing bitwise calculations; are we then to understand that &&= and ||= are more "woolly" in scope, handling the sort of Booleans an "if" statement would process, as oppose to actual Boolean (bitwise) operations? Hmmm...

• ##### Re: &&= and ||=

by Andrew Witte /

Your message is awaiting moderation. Thank you for participating in the discussion.

Do these have some actual value besides some super rare scenario doing it a different way isn't good enough?

• ##### Re: &&= and ||=

Your message is awaiting moderation. Thank you for participating in the discussion.

It's the short-circuit version, which matches how && and || work inside an if.

• ##### Re: &&= and ||=

Your message is awaiting moderation. Thank you for participating in the discussion.

Guys, as the title says, it's just a small feature.

C# has always rewritten "a op= b" as "a = a op b", for all the binary operators, but not && and ||. Now they are allowed.

So:

a &&= b is equivalent to a = a && b, which is the same as if (a) a = b, and

a ||= b is equivalent to a = a || b, which is if (!a) a = b

where a is a bool variable, and b a bool expression.

When will you use it? Not often, I suspect.

Steve

• ##### Re: &&= and ||=

Your message is awaiting moderation. Thank you for participating in the discussion.

booleans are not "bitwise". You're confusing two different things.

• ##### Re: &&= and ||=

Your message is awaiting moderation. Thank you for participating in the discussion.

When will you use it? Not often, I suspect.

They're quite handy -- I've been using them for a while now! (Just not in C# ;-)

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

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

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.