In this article, we will look into 5 new features in C# 10. .NET 6 was released on November 8, 2021, and C# 10 works with .NET 6.
C# 10 new features
C# 10 introduces below new features.
1. Record Structs
The keyword Record was introduced with C# 9. Record is used to define reference type which helps to achieve data encapsulation.
You can now clarify that a record is a reference type with the record class declaration in C# 10.
2. Nested property patterns
With C# 10 new feature you can reference nested properties or fields within a property pattern. For example –
{ Prop1.Prop2: pattern }
3. Global using directives
In C# 10, we can use global using directives to tell the compiler that the directives apply to all source file within the project.
Syntax to use global directives –
global using < fully - qualified -namespace>;
global using static System.Math;
4. File-scoped namespace
File-scoped namespace features in C# 10 allow us to declare that all types in a file are in a single namespace.
Before C# 10 –
namespace SampleNamespace
{
class SampleClass { }
interface ISampleInterface { }
}
In C# 10, the above code snippet can be written like the below –
using System;
namespace SampleFileScopedNamespace;
class SampleClass { }
interface ISampleInterface { }
5. Interpolated string handler
You can create a type that builds the output string from an interpolated string expression. An interpolated string handler is a custom type that converts the interpolated string into a string.
With C# 10, when an interpolated string is used, the compiler checks if the interpolated string is assigned to a type that satisfies the interpolated string handler pattern.
Apart from the above 5 new features in C# 10, there are a few other features like – improvement of structure types, improvement in lambda expressions, Improved definite assignment, CallerArgumentExpression attribute, and Enhanced #line pragma.
You may read this article – Evolution of .NET Framework to .NET Core