Implicit Usings in .NET 6


If you’re a .NET developer, you may have heard about the new feature in .NET 6 called “Implicit Usings”. This feature allows you to omit certain using statements in your C# code, making it easier to write and read. In this blog post, we’ll explore what Implicit Usings are, how they work, and how you can use them in your own .NET 6 projects.

What are Implicit Usings?

In previous versions of .NET, you had to include using statements at the beginning of each C# file to import the namespaces required for the code in that file. For example, if you wanted to use the Console.WriteLine method, you would need to include the following using statement at the beginning of your file:

using System;

However, in .NET 6, you can now use certain namespaces without explicitly including them in your code. These are called “Implicit Usings”. This means that the using statements for these namespaces are automatically added to your code by the compiler, without you having to write them yourself.

How do Implicit Usings work?

Implicit Usings are enabled by default in .NET 6 projects. When you create a new .NET 6 project, the compiler generates a file with the extension called “.globalusings.g.cs” in the “obj” folder of your project. This file contains a list of namespaces that are automatically imported into your code.

For example, the following namespaces are included by default in a .NET 6 console application:

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

This means that you can use the Console.WriteLine method without explicitly including the System namespace, because it is already included in the .globalusings.g.cs file.

How to use Implicit Usings in your code?

To use Implicit Usings in your code, you simply write your code as normal, without including the relevant using statements. For example, to use the Console.WriteLine method, you can write:

Console.WriteLine("Hello, world!");

Instead of:

using System;



Console.WriteLine("Hello, world!");

This can make your code easier to read, as you don’t have to include lots of using statements at the beginning of each file. However, it’s important to note that not all namespaces are included in the .globalusings.g.cs file, so you may still need to include using statements for some namespaces.

Customizing Implicit Usings

If you want to customize the Implicit Usings in your project, you can do so by editing the .csproj file. To disable Implicit Usings, you can add the following line to your .csproj file:

<ImplicitUsings>disable</ImplicitUsings>

If you want to customize the Implicit Usings, you can add an ItemGroup to your .csproj file, like so:

<ItemGroup>
  <ImplicitUsing Include="System.Xml.Linq" />
  <ImplicitUsing Remove="System.IO" />
</ItemGroup>

This will add the System.Xml.Linq namespace to the list of Implicit Usings, and remove the System.IO namespace from the list.

In the Conclusion Implicit Usings are a new feature in .NET 6 that can make your code easier to read and write. By automatically including certain using statements, you can write more concise code without sacrificing clarity. If you’re starting a new .NET 6 project, it’s worth taking a look at Implicit Usings to see how they can help you write better code.


You may also like...

Leave a Reply

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