C# 10 The Main New Features Explained in Less Than 5 Minutes

Abdelmajid BACO
3 min readDec 27, 2022

--

from https://github.com/dotnet/brand

C# 10 was officially released on September 18th, 2020 as part of .NET 6.

In this article, I will discuss some of the new features that are worth exploring.

Record Structs

Record structs are a new feature in C# 10 that allow developers to create immutable data types using concise syntax. With record structs, developers can quickly define and initialize immutable data that is easy to read and maintain. Record structs are value types while Record classes, which were introduced in C# 9, are reference types.

public record struct Point
{
public double X { get; init; }
public double Y { get; init; }
public double Z { get; init; }
}

Global Using Directives

Global using directives allow developers to include the same set of using statements on all files within a project. This eliminates the need to manually write the same using statement(s) at the top of every class file, streamlining development and making it easier to access namespaces and their members.

global using Xunit;

File-Scoped Namespace

File-scoped namespace is a feature in C# 10, where you can declare namespaces within the scope of a single file without the need for braces. The intent of this feature is mainly to satisfy the requirements of the overwhelming majority of the ecosystem with less extra unnecessary code.

namespace A.B.C;

using System;

class M
{
}

Extended Property Patterns

Extended property patterns are a feature introduced in C# 10 that enhances the syntax of property patterns. This allows developers to access the property values of objects in a more efficient way when using pattern matching statements. This feature also increases the usability of pattern matching when dealing with complex conditions.

Person person = new();

//C# 10
if (person is { FistName: "Victor", Adress.City: "Paris" })
{
Console.WriteLine("Victor lives in Paris");
}
//C# 9
if (person is { FistName: "Victor", Adress : { City: "Paris" } })
{
Console.WriteLine("Victor lives in Paris");
}

class Person
{ public string FistName { get; set; } = "Victor";
public string LastName { get; set; } = "Hugo";
public Adress Adress { get; set; } = new();
}
class Adress
{
public string City { get; set; } = "Paris";
public string Country { get; set; } = "France";
}

Assignment and Declaration in The Same Deconstruction

Assignment and declaration in the same deconstruction is a feature that allows developers to assign one value to a variable while simultaneously declaring the variable itself. Before this feature, a variable had to be declared before it could be assigned a value, but now this step can be done in a single line of code.

// C# 9
var point = (X: 10, Y: 12);
(int x, int y) = point;

// assignment:
int x1 = 0;
int y1 = 0;
(x1, y1) = point;

// C# 10
int x = 0;
(x, int y) = point;

CallerArgumentExpression Attribute Diagnostics

The CallerArgumentExpression attribute in C# 10 can be used in diagnostics and debugging to give developers more information about the arguments that were passed to a method. This helps developers quickly identify any issues or inaccuracies in their code and makes troubleshooting much easier.

using System.Runtime.CompilerServices;

Validate(1 < 0);

void Validate(bool condition, [CallerArgumentExpression("condition")] string? message = null)
{
if (!condition)
{
throw new InvalidOperationException($"Argument failed validation: <{message}>");
}
}

Conclusion

In conclusion, C# 10 brings a number of new features and improvements to the language. These features give developers more control over their code, allowing them to write cleaner, more efficient code. Additionally, they enable developers to access more information while debugging and diagnostics, helping them quickly identify and resolve any issues.

Thank you for taking the time to read this article!

Resources

--

--

Abdelmajid BACO

Senior Full Stack .Net / Angular Developer, Cloud & Azure DevOps, Carrier Manager, Husband, and Father.