C# 10.0 New Features

Raymond Raymond event 2021-08-14 visibility 585
more_vert
C# 10.0 New Features

.NET 6.0 preview 7 was published on 2021-08-10. Together with .NET 6, C# 10 with new preview language features are also available. This article will provide some examples about these new features.

Install .NET 6.0 SDK

Download .NET 6.0 SDK from this link: Download .NET 6.0 (Linux, macOS, and Windows). You can also install it when installing Visual Studio 2022.

Verify the installation using the following command line:

dotnet --list-sdks
2.0.0 [C:\Program Files\dotnet\sdk]
2.1.202 [C:\Program Files\dotnet\sdk]
2.1.400 [C:\Program Files\dotnet\sdk]
3.0.100 [C:\Program Files\dotnet\sdk]
3.1.101 [C:\Program Files\dotnet\sdk]
3.1.117 [C:\Program Files\dotnet\sdk]
3.1.200 [C:\Program Files\dotnet\sdk]
3.1.400 [C:\Program Files\dotnet\sdk]
5.0.100-rc.1.20452.10 [C:\Program Files\dotnet\sdk]
5.0.103 [C:\Program Files\dotnet\sdk]
5.0.205 [C:\Program Files\dotnet\sdk]
5.0.302 [C:\Program Files\dotnet\sdk]
5.0.400 [C:\Program Files\dotnet\sdk]
6.0.100-preview.7.21379.14 [C:\Program Files\dotnet\sdk]
Check the current version in use:

dotnet --version
6.0.100-preview.7.21379.14

Now let's start to create a new project to explore these C# new features.

Create a dotnet console project

Use the following commands to create a Console application using .NET 6.0 SDK.

mkdir csharp10
cd csharp10
dotnet new console

There are two files created:

Program.cs  csharp10.csproj

Run the following command to compile and execute the application:

dotnet build
dotnet run

The output looks like this:

Hello World!

Now open this project in Visual Studio Code to code easily:

code .

The project looks like the following screenshot in VS Code:

20210814114327-image.png

Now let's start to explore these new features in C# 10. 

Constant interpolated strings

Now constant interpolated strings are allowed as long as the referenced variables themselves are constants. 

The following code snippet shows how to use this new feature:

const string i = "100";
const string text = "Hello Kontext!";
const string interpolated = $"i={i},text='{text}'";
Console.WriteLine(interpolated);
Console.ReadLine();

When building it in previous SDKs, the following error will throw out:

The expression being assigned to 'interpolated' must be constant [csharp10]csharp(CS0133)

Now with .NET 6, we can turn on this new feature on by adding the following node to your project file:

  <PropertyGroup>
   <LangVersion>preview</LangVersion>
  </PropertyGroup>

Refer to C# language versioning - C# Guide | Microsoft Docs for more details about defining C# language version in projects.

warning Note: If we change constant i to int type, the compiler error CS0133 will still show up as only constant strings are allowed for this feature since int variable will be converted to string as runtime while this feature is a compiler time feature.

Seal ToString method for record type

Record type was introduced from C# 9.0 and can be commonly used in data analytics projects using C# as programming language. From C# 10 sealed modifier can be added to ToString method to ensure all derived record types use the method defined in base type. 

Let's examine the following code snippet:

var customer = new Customer("Raymond", "Tang");
var busCustomer = new BusinessCustomer("Raymond", "Tang", "Example business");
Console.WriteLine(customer);
Console.WriteLine(busCustomer);
Console.ReadLine();

public record Customer
{
    public string LastName { get; }
    public string FirstName { get; }

    public Customer(string first, string last) => (FirstName, LastName) = (first, last);

    public override string ToString()
    {
        return $"{FirstName} {LastName}";
    }
}

public record BusinessCustomer : Customer
{
    public string BusinessName { get; }
    public BusinessCustomer(string first, string last, string business) : base(first, last)
    {
        BusinessName = business;
    }
}

The output of the program looks like the following text:

Raymond Tang
BusinessCustomer { LastName = Tang, FirstName = Raymond, BusinessName = Example business }

As we can tell from the output, the derived record type BusinessCustomer didn't inherit ToString() function from Customer record type.

From C# 10.0, we can add sealed modifier to the ToString() function:

public override sealed string ToString()
    {
        return $"{FirstName} {LastName}";
    }

When rerun the program, the output becomes like this:

Raymond Tang
Raymond Tang

This feature also requires enablement of preview language features.

Both assignment and declaration in the same deconstruction

C# 10 now allows both assignment and declaration in the same deconstruction.

var point = (1, 1);
int x = 0;
(x, int y) = point;
Console.WriteLine($"{x} - {y}");

Output:

1 - 1

This feature doesn't require preview language feature in preview 7.

Allow AsyncMethodBuilder attribute on methods

From C# 10, you can also added AsyncMethodBuilder attribute to async methods in addition to a type (C# 7.0).

Refer to C# reserved attributes: Miscellaneous | Microsoft Docs for more details.

Global using

Instead of adding using statements to import namespaces in each code file, you can now add global using statements.

For example, add the following file Usings.cs in the project will import System namespace for all code scripts in the project:

global using System;

This can potentially save much time to write code. For more details, refer to Global using directive - C# 10.0 draft specifications | Microsoft Docs.

File scoped namespace

Instead of using brackets, you can now define file scoped namespace. For more details, refer File scoped namespaces - C# 10.0 draft specifications | Microsoft Docs.

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace csharp10;

class MyClass
{
}

References

More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts