C# 10 Preview: Global Usings

C# 10 (coming with .NET 6) aims to reduce file clutter. **Global Usings** allow you to define namespace imports once for the entire project.

Manual Creation

Create a file `GlobalUsings.cs`:

global using System;
global using System.Collections.Generic;
global using System.Threading.Tasks;
global using Microsoft.EntityFrameworkCore;

Implicit Usings

The SDK can do it for you based on project type.

<PropertyGroup>
    <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

This automatically imports `System.Net.Http.Json`, `Microsoft.AspNetCore.Builder`, etc., in Web projects.

Key Takeaways

  • Reduces vertical noise in every file.
  • You can `global using static System.Console;` to just write `WriteLine(“Hi”);` everywhere.

Discover more from C4: Container, Code, Cloud & Context

Subscribe to get the latest posts sent to your email.

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.