What's new in C# 10.0

Important

This article discusses the features available in C# 10.0 as of .NET 6 preview 7. Documenting the enhancements for C# 10.0 is in progress. You can check this project for progress on documentation.

C# 10.0 adds the following features and enhancements to the C# language (as of .NET 6 Preview 7):

Some of the features you can try are available only when you set your language version to "preview". These features may have more refinements in future previews before .NET 6.0 is released.

C# 10.0 is supported on .NET 6. For more information, see C# language versioning.

You can download the latest .NET 6.0 SDK from the .NET downloads page. You can also download Visual Studio 2022 preview, which includes the .NET 6.0 preview SDK.

Improvements of structure types

C# 10.0 introduces the following improvements related to structure types:

Global using directives

You can add the global modifier to any using directive to instruct the compiler that the directive applies to all source files in the compilation. This is typically all source files in a project.

File-scoped namespace declaration

You can use a new form of the namespace declaration to declare that all subsequent declarations are members of the declared namespace:

namespace MyNamespace;

This new syntax saves both horizontal and vertical space for the most common namespace declarations.

Extended property patterns

Beginning with C# 10.0, you can reference nested properties or fields within a property pattern. For example, a pattern of the form

{ Prop1.Prop2: pattern }

is valid in C# 10.0 and later and equivalent to

{ Prop1: { Prop2: pattern } }

that is valid in C# 8.0 and later.

For more information, see the Extended property patterns feature proposal note. For more information about a property pattern, see the Property pattern section of the Patterns article.

Constant interpolated strings

In C# 10.0, const strings may be initialized using string interpolation if all the placeholders are themselves constant strings. String interpolation can create more readable constant strings as you build constant strings used in your application. The placeholder expressions can't be numeric constants because those constants are converted to strings at runtime. The current culture may affect their string representation. Learn more in the language reference on const expressions.

Note

When using .NET 6.0 preview 5, this feature requires setting the <LangVersion> element in your csproj file to preview.

Record types can seal ToString

In C# 10.0, you can add the sealed modifier when you override ToString in a record type. Sealing the ToString method prevents the compiler from synthesizing a ToString method for any derived record types. This ensures all derived record types use the ToString method defined in a common base record type. You can learn more about this feature in the article on records.

Note

When using .NET 6.0 preview 5, this feature requires setting the <LangVersion> element in your csproj file to preview.

Assignment and declaration in same deconstruction

This change removes a restriction from earlier versions of C#. Previously, a deconstruction could assign all values to existing variables, or initialize newly declared variables:

// Initialization:
(int x, int y) = point;

// assignment:
int x1 = 0;
int y1 = 0;
(x1, y1) = point;

C# 10.0 removes this restriction:

int x = 0;
(x, int y) = point;

Note

When using .NET 6.0 preview 5, this feature requires setting the <LangVersion> element in your csproj file to preview.

Allow AsyncMethodBuilder attribute on methods

In C# 10.0 and later, you can specify a different async method builder for a single method, in addition to specifying the method builder type for all methods that return a given task-like type. This enables advanced performance tuning scenarios where a given method may benefit from a custom builder.

To learn more, see the section on AsyncMethodBuilder in the article on attributes read by the compiler.