BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Preview of C# 8.x

Preview of C# 8.x

Leia em Português

This item in japanese

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.

Rate this Article

Adoption
Style

BT