DEV Community

Cover image for .NET 6 is READY to launch!๐Ÿš€ Revealing 5 NEW (and awesome) FEATURES ๐Ÿคฏ
ByteHide
ByteHide

Posted on • Updated on

.NET 6 is READY to launch!๐Ÿš€ Revealing 5 NEW (and awesome) FEATURES ๐Ÿคฏ

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);
Enter fullscreen mode Exit fullscreen mode

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 }
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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 or record 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 or readonly 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 or class 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;
Enter fullscreen mode Exit fullscreen mode

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 use the new namespace declaration form to declare that all subsequently declared declarations are members of the declared namespace ๐Ÿ‘‡

namespace NamespaceName;
Enter fullscreen mode Exit fullscreen mode

Before it was like this ๐Ÿ‘‡

namespace NamespaceName
{
}
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

โšก 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 } }
Enter fullscreen mode Exit fullscreen mode

And now it can be done perfectly well like this ๐Ÿ‘‡

{ Prop1.Prop2: pattern }
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

๐ŸŽ 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 ๐ŸŽ.

Dotnetsafer Security Book

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! ๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡

Download FREE guide ๐ŸŽ


โœ… .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)

Collapse
 
ascot31 profile image
Alvaro Brena

Thanks ๐Ÿฆ„

Collapse
 
dave225 profile image
davidaboukaram

Nice Article ๐Ÿฆ„

Collapse
 
ematteau14 profile image
ematteau14

Thanks for those highlights and examples! ๐Ÿฆ„โ˜•