Global usings in C# 10

One of the new features in .NET 6 and C¤ 10 is support for global usings. We can define using declarations as global and they work all over the project so we don’t have to define them in other files anymore. Here’s how it works.

Sample project

global-usings-solutionI start with .NET 5.0 console application ported to .NET 6.0 so I have this normal Program.cs file with namespace and Program class. I really don’t like this new age script style stuff that comes with .NET 6.0. It’s good when teaching students to code in C# but in practice I see it as pretty useless feature.

Suppose we have have console program that runs on Task Scheduler and performs some nightly tasks. Screenshot of Solution Explorer on right illustrates what we have. There are some classes to access local file system and cloud storage. There are also some classes to perform import tasks.

Classes that move data from one place to another usually have some repeated using directives. One good example is System.Data and its child namespaces. Everytime we add new class we have to add those using directives again. This holds also true for importer classes shown on screenshot.

Here’s the fragment on one importer class.

using System.Data;
using System.Data.SqlClient;
using ConsoleApp4.FileAccess;
namespace ConsoleApp4.ImportExport;
public class CustomersImporter
     private readonly IFileClient _fileClient;
     private readonly string _connectionString;

     public CustomersImporter(IFileClient fileClient, string connectionString)
         _fileClient = fileClient;
         _connectionString = connectionString;

     // ... }

Three using declarations – first three lines of code above – are present also in other import and export classes.

Introducing global usings

C# 10 introduces global usings that are effective over whole project. Let’s add new file GlobalUsings.cs to our project. In this file we define usings that must be available in every class and interface file in our project.

global using System.Data;
global using System.Data.SqlClient;
global using ConsoleApp4.FileAccess;

We can remove these usings from all other files in our project.

Don’t put global usings to same file with class or interface. It’s easy to create a mess when global usings are defined in same file with classes or interfaces. Always keep global usings in separate file.

ASP.NET Core 6 application

Global usings work also in ASP.NET Core applications. There’s _ViewImports.cs file in ASP.NET Core where we can define usings for all views in project. Here’s the example of _ViewImports.cs file after creating a new ASP.NET Core MVC project.

@using WebApplication1
@using WebApplication1.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

Global usings work also for ASP.NET Core MVC views and Razor pages. Instead of keeping usings in _ViewImports.cs file we can make them global in global usings file.

global using System.Diagnostics;
global using Microsoft.AspNetCore.Mvc;
global using Microsoft.AspNetCore.Mvc.RazorPages;

Not so fast… Before abandoning _ViewImports.cs for using declarations think for a moment. Views can be taken as a separate context from all other code in your web application. Using declarations for views may be not needed in code files of your application. Don’t pollute your classes with using declarations you only use in views.

Wrapping up

Global usings is useful feature introduced in C# 10. It may also come specially handy when working with code that uses (too) many namespaces. Global usings are also good feature to keep code files cleaner and define all most needed usings in same file. Luckily it works also with ASP.NET Core MVC views and Razor pages.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    Leave a Reply

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