.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 opt…


This content originally appeared on DEV Community and was authored by Dotnetsafer

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 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;

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;

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
}

✅ .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!!! 😃👍


This content originally appeared on DEV Community and was authored by Dotnetsafer


Print Share Comment Cite Upload Translate Updates
APA

Dotnetsafer | Sciencx (2021-10-18T14:14:14+00:00) .NET 6 is READY to launch!🚀 Revealing 5 NEW (and awesome) FEATURES 🤯. Retrieved from https://www.scien.cx/2021/10/18/net-6-is-ready-to-launch%f0%9f%9a%80-revealing-5-new-and-awesome-features-%f0%9f%a4%af/

MLA
" » .NET 6 is READY to launch!🚀 Revealing 5 NEW (and awesome) FEATURES 🤯." Dotnetsafer | Sciencx - Monday October 18, 2021, https://www.scien.cx/2021/10/18/net-6-is-ready-to-launch%f0%9f%9a%80-revealing-5-new-and-awesome-features-%f0%9f%a4%af/
HARVARD
Dotnetsafer | Sciencx Monday October 18, 2021 » .NET 6 is READY to launch!🚀 Revealing 5 NEW (and awesome) FEATURES 🤯., viewed ,<https://www.scien.cx/2021/10/18/net-6-is-ready-to-launch%f0%9f%9a%80-revealing-5-new-and-awesome-features-%f0%9f%a4%af/>
VANCOUVER
Dotnetsafer | Sciencx - » .NET 6 is READY to launch!🚀 Revealing 5 NEW (and awesome) FEATURES 🤯. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/18/net-6-is-ready-to-launch%f0%9f%9a%80-revealing-5-new-and-awesome-features-%f0%9f%a4%af/
CHICAGO
" » .NET 6 is READY to launch!🚀 Revealing 5 NEW (and awesome) FEATURES 🤯." Dotnetsafer | Sciencx - Accessed . https://www.scien.cx/2021/10/18/net-6-is-ready-to-launch%f0%9f%9a%80-revealing-5-new-and-awesome-features-%f0%9f%a4%af/
IEEE
" » .NET 6 is READY to launch!🚀 Revealing 5 NEW (and awesome) FEATURES 🤯." Dotnetsafer | Sciencx [Online]. Available: https://www.scien.cx/2021/10/18/net-6-is-ready-to-launch%f0%9f%9a%80-revealing-5-new-and-awesome-features-%f0%9f%a4%af/. [Accessed: ]
rf:citation
» .NET 6 is READY to launch!🚀 Revealing 5 NEW (and awesome) FEATURES 🤯 | Dotnetsafer | Sciencx | https://www.scien.cx/2021/10/18/net-6-is-ready-to-launch%f0%9f%9a%80-revealing-5-new-and-awesome-features-%f0%9f%a4%af/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.