Friday, April 25, 2025

Visual Studio: C# Project-Level Global Usings

Visual Studio 2022 and its support for C# 10.0 and .NET 6.0 allow using statements to be placed at the project level (using the syntax global using <namespace>;), stored within the project’s Properties. To access this feature, right-click a project in Solution Explorer, select Properties from the context menu, and then choose the Global Usings tab:


Using the above cleans up individual C# files, which can become pointlessly cluttered, as shown in the following example:

using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System;

It is possible to have a standalone C# file that serves the same purpose, such as a per-project GlobalUsing.cs:

global using Microsoft.AspNetCore.Http;
global using Microsoft.Extensions.Logging;
global using System.Collections.Generic;
global using System.IO;
global using System.Threading.Tasks;
global using System;

A dedicated file, such as GlobalUsing.cs, is cleaner than per-source-file using statements. However, setting project-level global usings remains the cleanest approach.

As always, beware of the following, should some malevolent entity disable the feature for your project:





No comments :

Post a Comment