Simplifying Namespace Imports in C# with Global Using Directives

When working with C# code, developers often find themselves using a multitude of namespaces to access various classes and functions. Traditionally, each C# file would start with explicit “using” statements to import the required namespaces. However, this practice can quickly clutter the codebase, especially when dealing with large projects with numerous namespaces to include.

To address this issue, C# 10 and .NET SDK 6 introduced a new feature known as “global using” directives. This powerful feature allows developers to implicitly import namespaces globally, significantly reducing the need for repetitive “using” statements in individual files.

The Global Using Keyword Combination

With the introduction of the global using keyword combination, developers can import a namespace in a single .cs file and make it available throughout all other .cs files in the project. Previously, developers had to include common namespaces like “System” and “System.Linq” at the beginning of almost every .cs file. However, now, they can add the following code to a separate file, such as GlobalUsings.cs:

global using System;
global using System.Linq;
global using System.Collections.Generic;

This GlobalUsings.cs file contains all the global using statements, and any project that targets .NET 6.0 or later automatically benefits from these global imports.

Implicitly Imported Namespaces

When using projects that target .NET 6.0 or later, the C# 10 compiler generates a .GlobalUsings.g.cs file in the obj\Debug\net7.0 folder. This file implicitly imports some common namespaces that are frequently used in various projects. The specific list of implicitly imported namespaces depends on the SDK targeted.

The naming convention for the automatically generated file is .GlobalUsings.g.cs, with the “g” standing for “generated” to differentiate it from files written by developers.

Customizing Implicitly Imported Namespaces

Developers can customize the implicitly imported namespaces based on their project requirements. This can be achieved by modifying the project file to include or remove specific namespaces.

To add or remove implicitly imported namespaces, open the project file (typically with a .csproj extension) and add an section containing elements:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>
  <ItemGroup>
    <Using Remove="System.Threading" />
    <Using Include="System.Numerics" />
  </ItemGroup>
</Project>

In the above example, the System.Threading namespace is removed, and the System.Numerics namespace is included in the implicitly imported namespaces. This customization allows developers to fine-tune their project’s default namespace imports.

Disabling Implicitly Imported Namespaces

If a developer wishes to disable the implicitly imported namespaces feature altogether, they can remove the <ImplicitUsings> element completely from the project file or set its value to “disable”:

<ImplicitUsings>disable</ImplicitUsings>

Controlling Project Settings in Visual Studio 2022

For developers using Visual Studio 2022, project settings can be controlled through the user interface. To modify project settings:

  1. In Solution Explorer, right-click the project and select Properties.
  2. Click on the “Build” tab, and you’ll find the General section opened by default.

By leveraging global using directives, C# developers can significantly streamline their codebase, enhance readability, and reduce the effort required to import namespaces across multiple files. Embracing this new feature in C# 10 and .NET SDK 6 can lead to more maintainable and efficient codebases in ASP.NET Core and other C# projects.


Posted

in

by

Comments

Leave a comment