C# 10 – What’s new!

Tags:
C# 10

C# 10 is here with new and exciting feature with an opt-in approach. In this guide we will go through some of the most important changes that will make your code cleaner, smaller, faster and will hopefully convince you to upgrade now!

Microsoft announce C# 10 a few days ago, on November 8th 2021, as part of .NET 6 and Visual Studio 2022 general availability. It cannot be installed on each own and it requires more than .NET 6 (also VS2022) because of the conditional, opt-in, features that were added. If you are not convinced yet by the fact that .NET 6 is the fastest ever .NET which massively lowers hosting costs, let’s take a closer look in C# 10 and you might change your mind!

Global and implicit usings

C# 10 includes a new global using directive and implicit usings to reduce the number of usings you need to specify at the top of each file.

Global using directives

If the keyword global appears prior to a using directive, that using applies to the entire project:

Note that you can use any feature of using within a global using directive.

You can put global using in any .cs file, including Program.cs or a specifically named file like globalusings.cs.

For more information, see global using directives.

Implicit usings

Implicit usings are not exactly implicit, since it’s a feature that automatically adds common global using directives for the type of project you are building. To enable implicit usings set the ImplicitUsings property in your .csproj file:

The specific set of global using directives included depend on the type of application you are building. For example, implicit usings for a console application or a class library are different than those for an ASP.NET application.

For more information, see this implicit global usings.

Combining using features

I am not a big fan of hidden code to be honest but this time, traditional using directives, global using directives, and implicit usings work well together. The problem is, that regardless how careful you are, the increased number of using directives that are injected without you controlling them in every .cs file, increase the chance of ambiguity in name resolution.

If you encounter this, besides adding aliases or reducing the number of namespaces you are importing, you have been given some tools to elegantly overcome it.

If you need to remove namespaces that have been included via implicit usings, you can specify them in your project file like this:

You can also add namespace that behave as though they were global using directives, for example:

File-scoped namespaces

Many files contain code for a single namespace. Starting in C# 10, you can include a namespace as a statement, followed by a semi-colon and without the curly brackets:

This simplifies the code and removes a level of nesting. Only one file-scoped namespace declaration is allowed, and it must come before any types are declared.

Improvements for lambda expressions and method groups

C# 10 contains several improvements to both the types and the syntax surrounding lambdas.

Natural types for lambdas

Lambda expressions can have, from now on, a “natural” type, which means that the compiler can often infer the type of the lambda expression.

For example, up until C# 9 we had to convert a lambda expression to a delegate or an expression type (Func<> or Action<>), like this one:

Starting with C# 10, however, if a lambda does not have such a “target type” a computed will be used:

Hovering over var will show that the type is still Func<string, int>.

The compiler will use an available Func or Action delegate, if a suitable one exists. Otherwise, it will synthesize a delegate type (for example, when you have ref parameters or have a large number of parameters).

Unfortunately, not all lambdas have natural types because some just don’t have enough type information. For instance, leaving off parameter types will leave the compiler unable to decide which delegate type to use:

Finally, expression trees require “target” typing. If, for example, the target type is Expression and the lambda has a natural delegate type D an Expression<D> will be produced. For example:

Natural types for method groups

Method groups can have a natural type if that group has only one overload (which is automatically chosen as the type). For example:

Can from now on be written:

Return types for lambdas

When the return type of a lambda expression is obvious then it is just being inferred, but this is not always the case. For example the next code will throw an “Can’t infer return type” error.

In C# 10 though, you can explicitly specify a return type on a lambda expression:

Attributes on lambdas

Starting in C# 10, you can put attributes on lambda expressions in the same way you do for methods and local functions. They go right where you expect; at the beginning. Once again, the lambda’s parameter list must be parenthesized when there are attributes:

Just like local functions, attributes can be applied to lambdas if they are valid on AttributeTargets.Method.

Attributes do not have any effect when the lambda is invoked, but are still useful for code analysis, and they are also emitted on the methods that the compiler generates under the hood for lambdas, so they can be discovered via reflection.

Improvements to structs

C# 10 introduces features for structs that provide better parity between structs and classes. These new features include parameterless constructors, field initializers, record structs and with expressions.

Parameterless struct constructors and field initializers

Prior to C# 10, a parameterless constructor for a struct was not possible, since every struct had an implicit public parameterless constructor that set the struct’s fields to default.

Starting in C# 10, you can optionally write your own parameterless struct constructor that sets fields to any value.


You can initialize fields in a parameterless constructor as above, or you can initialize them via field or property initializers:

Structs that are created via default or as part of array allocation ignore explicit parameterless constructors, and always set struct members to their default values. For more information about parameterless constructors in structs, see the struct type.

record structs

Starting in C# 10, records can now be defined with record struct. These are similar to a record class that were introduced in C# 9:

You can continue to define record classes with just record as they were introduced, or you can use record class for clarity.

Structs already had value equality, so when you compare them it is by value. Record structs add IEquatable<T> support and the == operator. Record structs provide a custom implementation of IEquatable<T> to avoid the performance issues of reflection, and they include record features like a ToString() override.

Record structs can be positional with a primary constructor implicitly declaring public members. The difference is though, that this time fields be read/write (unlike record classes):

Implicitly created properties that are read/write, makes it easier to convert tuples to named types, clean up your code and guarantee consistent member names.

If you declare a property or field with the same name as a primary constructor parameter, no auto-property will be synthesized and yours will be used.

Immutability comes easy to a record struct, by adding the readonly keyword to that struct.

sealed modifier on ToString() in record classes

Starting in C# 10 the ToString() method can include the sealed modifier, which prevents the compiler from synthesizing a ToString implementation for any derived records.

with expressions on structs and anonymous types

C# 10 supports with expressions for all structs, including record structs, as well as for anonymous types:

This returns a new instance with the new value. You can update any number of values. Values you do not set will retain the same value as the initial instance.

Interpolated string improvements

C# 10 contains a few improvements on interpolated strings, which make things faster!

Interpolated string handlers

Today the compiler turns interpolated strings into a call to string.Format. This can lead to a lot of allocations – the boxing of arguments, allocation of an argument array, and of course the resulting string itself. In C# 10 a library pattern was added that allows an API to “take over” the handling of an interpolated string argument expression.

As an example, consider StringBuilder.Append:

Up until now, this would call the Append(string? value) overload with a newly allocated and computed string, appending that to the StringBuilder in one chunk. From C# 10, the strings "Hello ", args[0] and ", how are you?" will be individually appended to the StringBuilder, which is much more efficient and has the same outcome.

String.Create()

String.Create() in C# 10 lets you specify the IFormatProvider used to format the expressions in the holes of the interpolated string argument itself:

You can learn more about interpolated string handlers, in this article and this tutorial on creating a custom handler.

Constant interpolated strings

If all the holes of an interpolated string are constant strings, then the resulting string is now also constant. This lets you use string interpolation syntax in more places, like attributes:

Note that the holes must be filled with constant strings. Other types, like numeric or date values, cannot be used because they are sensitive to Culture, and can’t be computed at compile time.

Other improvements

C# 10 has a number of smaller improvements across the language. Some of these just make C# work in the way you expect.

Mix declarations and variables in deconstruction

Prior to C# 10, deconstruction required all variables to be new, or all of them to be previously declared. In C# 10, you can mix:

Extended property patterns

C# 10 adds extended property patterns to make it easier to access nested property values in patterns. For example, the following complex object can be pattern matched in both of the ways shown here:

The extended property pattern simplifies the code and makes it easier to read, particularly when matching against multiple properties.

Find out more about extended property patterns in the pattern matching article.

Not the end!

Although these were by far not all the changes C# 10 brings, they were the most important ones. Install .NET 6 or Visual Studio 2022, and comment out your favorite new feature!

4.5 2 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments