C# 10 Features

C# 10 Features: 10 New Features Of C# 10

In this post, we have listed the C# 10 Features. We will discuss in detail the features and enhancements added to the C# language as part of C# 10.0.

C# (CSharp)

C# is an object-oriented, type-safe, and managed language that is compiled by .NET Framework to generate Microsoft Intermediate Language (MSIL).

Interested to know how C# Evolved?
Check 👉  C# Version History 

C# 10 Features

C# 10 is supported on .NET 6 and Visual Studio 2022. The C# 10 features are:

  • Global usings
  • Null parameter checking
  • File-scoped namespaces
  • Constant interpolated strings
  • Attribute support generics
  • Record structs
  • Extended property patterns
  • Structure type improvements
  • Record types can seal ToString
  • Lambda improvements

Let’s try to understand each feature in detail with help of the code snippets.

1. Global usings

In C# 10, you can now declare your using statements with a global keyword. This means that the using directive preceding with the global keyword applies to the entire project.

global using System;

You can put global usings in any .cs file, including Program.cs or a specifically named file like GlobalUsings.cs.

Prior to C# 10, we had to specify the using directives at the top of each file.

2. Null parameter checking

We used to do the null checking similar to the code snippet shown below:

public UpdateAddress(int invoiceNumber, Address newAddress)
{
    if (newAddress == null)
    {
        throw new ArgumentNullException("newAddress");
    }
    ...
}

In C# 10, if you add !! to the end of your parameter (newAddress in the example below) then it automatically does the null checking for you.

public UpdateAddress(int invoiceNumber, Address newAddress!!)
{
    ...
}

3. File-scoped namespaces

In C# 10, you can follow the new way of a namespace declaration as shown below:

namespace MyNamespace;

In previous versions of the C# language, we used to declare the namespace as:

namespace MyNamespace.Services
{
    class MyClass
    {
... } }

However, C# 10 allows file-scoped namespace as shown below: Notice the removal of the brackets.

namespace MyNamespace.Services;
class MyClass
{
... }

4. Constant interpolated strings

Prior to C# 10, if a variable of type string was declared const we cannot use string interpolation.

The following statement was not valid in previous versions of C#

public const string GetById = $"{Base}/{{id:guid}}";

In order to make this work in previous versions, we had to write it like this:

public const string GetById = Base + "/{id:guid}";

However, now with C# 10, const strings may be initialized using string interpolation. So the statement below is perfectly valid in C# 10.

public const string GetById = $"{Base}/{{id:guid}}";

5. Attributes support generics

In C# 10, you can have generic attributes.

This is how you  can create a generic attribute:

public class DotnetCrunchAttribute : Attribute { }

Then, specify the type parameter to use the attribute:

[DotnetCrunchAttribute()]
public string Method() => default;

💡Note: At the time of writing this blog post, it is a preview feature. You must set <LangVersion> to Preview to enable this feature.

C# 10 Features

In the previous version of C#, you would need to create an attribute that takes a Type as its constructor parameter. C# 10 provides a more convenient syntax to achieve this.

6. Record structs

The record keyword was introduced in C# 9. However, it only supported the reference types i.e a class. Now with C# 10, you can only create record structs along with record classes.

public record struct Student(string FirstName, string LastName);

If you do not explicitly specify the class keyword, C# treats it as a class and not struct.

7. Extended property patterns

With C# 10, you can now reference nested properties or fields within a property pattern. Let’s have a look at it with the help of an example snippet.

File: Student.cs

public record Student(int studentID, string Name, Student? student = null);

File: Program.cs

var studentInfo = new Student(1009, "Bob");
var student = new Student(1009, "Bob", studentInfo);
if (student is { student: { Name: "Bob" } })
{
... }

In C# 10, this can be written as: Name is used as a property

if (student is { student.Name: "Bob" })
{
... }

8. Structure type improvements

In C# 10, there are a few improvements to the structure type.

  • You can declare and initialize a parameterless constructor in a structure type.
  • A left-hand operand of the with expression can be of any structure type. In C# 9, it must be of record type.

The parameterless constructor as shown in the example snippet below is perfectly valid in C# 10.

public readonly struct Customer
{
    public Customer()
    {
        Id = 0;
        Name = "Undefined";
    }

    public Customer(int id, string name)
    {
        Id = id;
        Name = name;
    }

    public int Id { get; init; }
    public string Name { get; init; }

    public override string ToString() => $"{Id} ({Name})";
}

9. Record types can seal ToString

In C# 10, While working with a record type, you can add the sealed modifier when you override the ToString method.

Sealing the ToString method prevents the method from overriding in the derived record type.

10. Lambda improvements

In C# 10 there are many improvements to lambda expressions:

  • Attributes can be applied to the lambda expressions.
  • Lambda expressions may have a natural type, where the compiler can infer a delegate type from the lambda expression.
  • Lambdas expressions may have ab explicit return type

For more, please refer 👉 Lambda improvements.

So these were the 10 new C# 10 Features that we tried to explain with help of examples. There are more new features, please refer to the further reading section.

C# 10 Features Video

We have created a video out of this blog post. In case, you prefer watching videos over reading the blog, you can watch this video and SUBSCRIBE to our youtube channel.

C# Course Recommendation

One of the best courses to master your C# skills right from level zero. This series is divided into 4 sections:

  1. C# Basics
  2. C# Intermediate
  3. C# Advanced
  4. Unit Testing for C# Developers

csharp mastery course mosh

 

Further Reading

Hope that you like our C# 10 Features post. You may refer to the official documentation available on C# 10.0 from Microsoft.

PS: If you found this content valuable and want to return the favor, then Buy Me a Coffee at ko-fi.com

Disclaimer: Most of the content is FREE. But some resources may contain affiliate links. When you purchase, I may receive a commission from the seller, at no extra cost to yourself.

Happy Coding {}

Disclaimer: Most of the content is FREE. But some resources may contain affiliate links. When you purchase, I may receive a commission from the seller, at no extra cost to yourself.

Share with your friends:

Leave a Comment

Your email address will not be published. Required fields are marked *