Skip to content

5+1 Features of C# 10 you need to know NOW

27 de September de 2021
csharp 9 csharp 10

With only two months left until the official release of C# 10 and Microsoft has opened a discussion about the new features and functions of the tenth version of its C# language. These enhancements are those announced in C# 10.0 Preview 7.

The features and improvements that Microsoft is going to implement in the next version of C# 10 are the following 👇

Index

Record types can seal ToString

Now in C# 10.0 version, you have the ability to add the sealed modifier when you override ToString in a record type.

Sealing the ToString method evade the compiler from synthesizing a ToString method for any derived record types. This function allows you to ensure all derived record types use the ToString method defined in a common base record type.

Microsoft advises us that this feature requires setting the <LangVersion> element in the csproj file to preview.

What is the Record Keyword?

If you don’t know what I’m talking about, this is normally used to define a reference type that provides built-in functionality for encapsulating data.

A simple example of what can be done with this is that you can create record types with immutable properties using standard property syntax 👇

And what better way to understand it than with a simple example from Microsoft 👇

File-scoped namespace declaration

You can now use the new namespace declaration form to declare that all subsequently declared declarations are members of the declared namespace 👇

This new syntax, which will be implemented in the new version of C# 10, will save both vertical and horizontal space for the most common namespace declarations.

What is namespace keyword?

To clarify this, the namespace keyword is used to declare a scope that contains a set of related objects. You can use a namespace to organize code elements and to create globally unique types.

And sure, but…

What are file-scope namespace declarations?

These declarations in particular, allow you to declare all the types of a file, which are in a single namespace.

To go a little deeper, in this version of C# 10.0, the example is similar to the previous one that has been shown by Microsoft, but uses a file scope namespace declaration 👇

Constant interpolated strings

To understand the Constant interpolated strings, first we need to understand String Interpolation.

The $ character identifies a string literal as an interpolated string. An interpolated string is a string literal that might contain interpolation expressions.

When an interpolated string is resolved to a result string, items with interpolation expressions are replaced by the string representations of the expression results.

Perfect, now my question is….

What Constant interpolated strings have in C# 10?

Talking about this new feature that C# version 10.0 will bring is const strings, which can be initialized using string interpolation only if the placeholders are themselves constant strings.

String interpolations can create more readable const strings as the const strings used in the application are constructed. Placeholder expressions cannot be numeric constants because those constants are converted to strings at runtime. The culture currently in place, could affect the string representation.

Extended Property Patterns

First, I will explain what are the Extended property patterns. These patterns allow you to have property subpatterns refer to nested members, for example:

Instead of:

Now that this has been clarified….

What Extended Property Patterns have in C# 10?

The next functionality that Microsoft discusses is that nested fields or properties could be referenced within a property pattern. The example Microsoft gives is a pattern of the form 👇

Microsoft tells us that it will be valid in C# 10.0 and later, and that this is equivalent to 👇

And this is valid in C# version 8.0 and all subsequent versions

Declaration and assignment in same deconstruction

This new change implemented by the new version, allows to remove the restriction of previous versions of C#. The example that Microsoft gives us in this case is 👇

…Previously, a deconstruction could assign all values to existing variables, or initialize newly declared variables:

This restriction is eliminated in C# 10.0 👇

Global using directives

You can now use the global modifier to any using directive. With this you can tell the compiler that the directive must be applied to all source files in the compilation.

Perfect, but as before…

What is the using directive?

This directive allows you to use types defined in a namespace without specifying the entire namespace of that type.

To summarize, the using directive imports all types from a single namespace, as shown in the following example 👇

You can apply two modifiers to a using directive:

  • The global modifier has the same effect as adding the same using directive to every source file in your project. This modifier was introduced in C# 10.0.
  • The static modifier imports the static members and nested types from a single type rather than importing all the types in a namespace.
Settings