After many months of leaks, speculation and more, Microsoft officially releases .NET 6 RC2. This is the second of 2 versions to be โreleasedโ and supported in production.
This version brings many new features, both new features and performance and optimization improvements. Letโs take a look at The 5 Most Important .NET 6 Features.
๐ฅ C# 10
C# 10 right now Microsoft calls it one of the most important parts of .NET 6. Primarily, C# 10 is the evolution of what already exists, both in concepts and in 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.
There are so many new features of C# 10 that would give for a whole article since this one is only focused on .NET 6. So if you want to discover with me all the new features of C# 10, give me right now an unicorn ๐ฆ. If I see many you will make my day and I will publish an article EXCLUSIVELY about whatโs new in C# 10.
โจ Record structs
Finally the C# 10 release adds support for registry structs. To understand this new feature, it is very similar to the records that are in C# 9 version (class-based) but with quite a few differences.
The biggest change is that registry structs have been added for completeness, so 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.
To be clear, record structures DO NOT REPLACE record classes. As Microsoft tells us, they do not โencourageโ the migration from record classes to record structures. This guidance for the use of classes can be applied equally to registry structs as to registry classes themselves.
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
To understand the theory, what better way than to look at a practical example? Letโs do it ๐
Battery battery = new("CR2032", 0.235, 100);
WriteLine(battery);
while (battery.RemainingCapacityPercentage > 0)
{
battery.RemainingCapacityPercentage--;
}
WriteLine(battery);
public record struct Battery(string Model, double TotalCapacityAmpHours, int RemainingCapacityPercentage);
And this is what the executed code would return ๐
Battery { Model = CR2032, TotalCapacityAmpHours = 0.235, RemainingCapacityPercentage = 100 }
Battery { Model = CR2032, TotalCapacityAmpHours = 0.235, RemainingCapacityPercentage = 0 }
What you will notice now, is that it is very similar to the record example in C# 9.
Battery battery = new("CR2032", 0.235, 100);
WriteLine(battery);
while (battery.RemainingCapacityPercentage > 0)
{
Battery updatedBattery = battery with
{RemainingCapacityPercentage =
battery.RemainingCapacityPercentage - 1};
battery = updatedBattery;
}
WriteLine(battery);
public readonly record struct Battery(string Model, double TotalCapacityAmpHours, int RemainingCapacityPercentage);
To repeat again what has already been said, the main distinguishing feature of record struct properties (apart from the record struct syntax), is that they are mutable.
What are the main differences between struct records and class records? ๐ค
Record classes are defined with
record
orrecord class
.The properties of the
record class
are immutable (get/init) by default.Record struct properties are mutable (get/set) by default.
Records are defined with record
struct
orreadonly record struct
.
Why do Struct records look like Class records? ๐ค
Support
with
expressions.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
struct
orclass
in the definition).Allow to customize member behavior, using
init
or 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 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 ๐
using System.Text;
You can apply two modifiers to a using
directive:
The
global
modifier has the same effect as adding the sameusing
directive to every source file in your project. This modifier was introduced in C# 10.0.The
static
modifier imports thestatic
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 use the new namespace declaration form to declare that all subsequently declared declarations are members of the declared namespace ๐
namespace NamespaceName;
Before it was like this ๐
namespace NamespaceName
{
}
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.
Const
and interpolated strings
Now, interpolated strings can be assigned to const variables. These interpolated strings are intuitive to read and use. They should be usable everywhere. They can now be used with const as long as the placeholder values are also const.
Letโs look at the Microsoft example ๐
const string Bar = "Bar";
const string DoubleBar = $"{Bar}_{Bar}";
WriteLine(DoubleBar);
โก Extended property patterns
Right now, within a property pattern, you can reference nested fields. The best example (before) to understand it is this ๐
{ Prop1: { Prop2: pattern } }
And now it can be done perfectly well like this ๐
{ Prop1.Prop2: pattern }
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
๐
List<Status> statuses = new()
{
new(Category.Normal, new(20, false, 20)),
new(Category.Warning, new(20, false, 60)),
new(Category.Danger, new(20, true, 60)),
new(Category.Danger, new(100, false, 20))
};
foreach (Status status in statuses)
{
string message = status switch
{
{Category: Category.Normal} => "Let the good times roll",
{Category: Category.Warning, Reading.PM25: >50 and <100} =>
"Check the air filters",
{Reading.PM25: >200 } => "There must be a fire somewhere.
Don't go outside.",
{Reading.SmokeDetected: true } => "We have a fire!",
{Category: Category.Danger} => "Something is badly wrong",
_ => "Unknown status"
};
Console.WriteLine(message);
}
record struct Reading(int Temperature, bool SmokeDetected, int PM25);
record struct Status(Category Category, Reading Reading);
enum Category
{
Normal,
Warning,
Danger
}
๐ Do you want a gift?
If you are reading this, it means that you belong to the 1% of people who read the articles UNTIL THE END and for that you GET a GIFT๐!!!! But first you must give a unicorn ๐ฆ (So I will know who are the faithful ones who read the whole articles ๐).
Just for entering this article and making it this far, Iโm GIVING you a GIFT of a GUIDE to keeping your .NET applications secure ๐.
The ONLY thing you have to do is enter your email to receive it for FREE ๐ค.
๐จ This wonโt be around forever.
To maintain exclusivity, Iโll be editing the article soon and there will no longer be a gift. So HURRY UP and get your guide to becoming a .NET security expert! ๐๐๐
โ .NET 6 Conclusion
The new features of .NET 6 are many, most of them have not been fully exploited (even though this is already the RC 2 Preview) and we will have to wait for Microsoft to talk about them in depth in the not too distant future as we are a couple of weeks away from the official release in November.
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.โ
If you liked this article, donโt forget to FOLLOW US, so that you can be one of the first to read whatโs new in .NET.
And if you are reading this, it means that you belong to the 1% of the people who read the articles UNTIL THE END, tell me how many coffees โ you need per day to continue programming, if I see many coffees โ I will not feel alone and you will make my day!!! ๐๐
Top comments (3)
Thanks ๐ฆ
Nice Article ๐ฆ
Thanks for those highlights and examples! ๐ฆโ