Skip to content

5 New .NET 6 Features to be released officially next week

5 de November de 2021
dotnet 6 realeased new features

It’s November already! After months of rumors, speculations, and so on, Microsoft will finally reveal .NET 6 at .NET Conf next week.

This release includes lots of new features as well as speed and efficiency enhancements.

Let’s take a closer look at The 5 Most Important .NET 6 Features.

Index

C# 10

Right now, C# 10 It is regarded by Microsoft as one of the most critical components of .NET 6. C# 10 is mostly an extension of what currently existing, both in terms of concepts and capabilities and features, registries, or patterns.

To summarize a little what brings C# 10, the global using, the namespaces with file scope and more very good characteristics that will allow to simplify the code and to write less repetitions.

Record structs

Finally, registry structs are supported in the C# 10 version. To understand this new feature, compare it to the records in the C# 9 version (class-based), but with a few modifications.

The most significant change is the addition of registry structs for completeness, so that structs can enjoy the same registry benefits as classes.

But that’s not all, Microsoft did not only limit itself to struct records, but also, they decided to align class records as much as struct records with ValueTuple.

The result of this?

Well, the struct properties of records are mutable by default, while the class properties of records are immutable. Although it is still possible to declare a readonly record struct, which matches the semantics of the record class and is immutable.

Record structures, to be clear, DO NOT REPLACE record classes. According to Microsoft, the conversion from record classes to record structures is not “encouraged” . This class use recommendation applies equally to registry structs as it does to registry classes.

In Microsoft’s own words:

“…In other words, the choice between classes and structs must be made before choosing to use registers…”

Record structs in action

What better way to explain the theory than to look at a real example? Let’s get started:

And that’s what the code would yield if it were run:

You’ll see that it’s quite close to the record example in C# 9:

To reiterate, the key differentiating characteristic of record struct properties (apart from the record struct syntax) is that they are modifiable.

What are the main differences between struct records and class records?

  • Record classes are defined with recordor record class.
  • The properties of the record classare immutable (get/init) by default.
  • Record struct properties are mutable (get/set) by default.
  • Records are defined with record structor readonly record struct.

Why do Struct records look like Class records?

  • Support withexpressions.
  • They have the ability to customize member definitions (which is new in C# 10) to use fields instead of default property members.
  • The syntax used is the same (except structor class in the definition).
  • Allow to customize member behavior, using initor mutable properties.

Global usings

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 already mentioned…

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.

Let’s look at different types of syntaxes:

  • global using System;
  • global using static System.Console;
  • global using E = System.Environment;

File-scoped namespace declaration

You can now specify that all subsequent defined declarations are members of the declared namespace using the new namespace declaration form:

Before it was like this:

The improved syntax, which will be included in C# 10, will save both vertical and horizontal space for the most popular namespace declarations.

const and interpolated strings

Strings that have been interpolated can now be assigned to const variables. These interpolated strings are simple to understand and apply. They should be usable in any situation. They can now be combined with const if the placeholder values are likewise const.

Let’s look at the Microsoft example:

Extended property patterns

You may now reference nested fields using a property pattern. This is the finest example (before) to comprehend it:

And now it can be done perfectly well like this :

You can see this more compact form used in the following example by Microsoft taken from RC 2 of .NET 6, for example with Reading.PM25 :

.NET 6 Conclusion

The features have been added of .NET 6 are numerous; however, the majority of them have yet to be fully utilized (despite the fact that this is the RC 2 Preview), and we will have to wait for Microsoft to discuss them in detail in the not-too-distant long term, as we are only a few days away from the official release the next week.

Microsoft says:

“It’s inspiring to see the new features in .NET 6 that will lay the foundation for what’s coming next. These are big-bet features that will push the platform forward in both obvious and non-obvious ways.”

Settings