InfoQ Homepage News Preview of C# 8.x

Preview of C# 8.x

Leia em Português


Even though C# 8.0 is still months away, planning has begun for C# 8.x. Some of these features are new, while others were previously considered for C# 8. And as always, this list is subject to change.

As discussed in a previous report, the Caller Expression Attribute feature would allow a function to capture the expression used to generate its arguments. This would primarily be used in assertions and automated testing.

Type inference for the new keyword, formally known as Target-Typed new Expression, would reduce boilerplate code in situations where the name of the type can be determined by the context.

Though the current opinion is roughly 3:1 against this feature, the Defer statement is still on the list of features being considered for an 8.x release.

Null-Enhanced Common Type

This is actually two requests, #33 and #881, that remove the need for adding explicit casts when working with nullable value types and ternary operators. Under the Null-enhanced Common Type proposal, the following scenarios would be allowed.

int a;
int? x1 = condition ? a : null;

int? b;
double c;
double? x2 = condition ? b : c;

This improvement would also be supported when inferring the type of an array or return type of a lambda expression.

Declaration Expressions

A feature of C-based languages is that assignments are expressions, not just statements. This allows for shortcuts such as a = b = c = 0, but also mistakes such as if ( a = false). For this reason, many style guides prohibit the modification of a variable within another statement.

C# has been moving in the other direction with the ability to define variables in new places such as out parameters and as part of pattern matching. With declaration expressions, you will be able to declare variables almost anywhere you can perform an assignment.

Where previously you could write,

char ch;
while ((ch = GetNextChar()) == 'a' || ch == 'b' || ch == 'c')

you would be able to shorten it to

while ((char ch = GetNextChar()) == 'a' || ch == 'b' || ch == 'c')

Here is another example of what you would be able to do:

var sum = (var p = GetPoint(); p.X + p.Y);

A related feature is the ability to Mix Declarations and Variables in Deconstruction. First proposed for C# 7.1, this would allow multiple-assignments when some, but not all, of the variables are newly defined.

(x, var y) = e;

Local Function Enhancements

A limitation of local functions is that attributes cannot be applied to them (this is only a C# limitation; the CLR permits it). The reason this can be problematic in C# 8 is attributes such as NotNullWhenTrue may be needed to get the right semantics for nullability checks.

In another example, attributes on local functions can be used to configuring ASP.NET routing more conveniently.

We need your feedback

How might we improve InfoQ for you

Thank you for being an InfoQ reader.

Each year, we seek feedback from our readers to help us improve InfoQ. Would you mind spending 2 minutes to share your feedback in our short survey? Your feedback will directly help us continually evolve how we support you.

Take the Survey

Rate this Article


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

Community comments

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:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.