Skip to content

C# Global Using Directives — Complete Guide

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about C# Global Using Directives. We cover key concepts, practical examples, and best practices.

Every file in your project starts with the same five using directives — System, System.Collections.Generic, System.Linq, System.Threading.Tasks, and your own project namespaces. You copy-paste them into every new file. Global usings let you declare them once.

Wrong

// Every single file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MyApp.Data;

namespace MyApp.Services;
// ... actual code ...

Output: Repetitive boilerplate in 50+ files. If a namespace changes, you update 50 files.

// GlobalUsings.cs (or any file)
global using System;
global using System.Collections.Generic;
global using System.Linq;
global using System.Threading.Tasks;
global using MyApp.Data;

Now every file in the project has those namespaces imported automatically.

// UserService.cs — no usings needed
namespace MyApp.Services;
public class UserService
{
    public List<string> GetItems() => new();
}

Output: Compiles fine. The global using from GlobalUsings.cs is applied project-wide.

You can also use global using static for static members and global using alias for aliased namespaces.

Prevention

  • Create a dedicated GlobalUsings.cs file for all project-wide usings.
  • Use global using only for namespaces used in 80%+ of files.
  • Keep per-file using for rare dependencies.
  • The SDKs (ASP.NET, MAUI, etc.) generate implicit global usings automatically — do not duplicate them.
  • Use global using with aliases for disambiguation: global using Project = MyApp.Very.Long.Namespace;.
  • Remove redundant using directives from individual files — the global ones already cover them.

Common Mistakes with global using

  1. Using foldl instead of foldl' causing stack overflow on large lists
  2. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  3. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable

These mistakes appear frequently in real-world CSHARP code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

Where should I put global using directives?

Convention is a file named GlobalUsings.cs at the project root, but any .cs file works. The global modifier makes it project-wide regardless of file location. Avoid scattering them across multiple files.

Can I remove a global using in a specific file?

No. If a global using causes a conflict in a specific file, you must use fully qualified names or aliases to disambiguate. You cannot "un-import" a global using in a single file.

Do implicit usings conflict with global usings?

No. Implicit usings (generated by the SDK) are separate. If you global using the same namespace, it is harmless. If an implicit using conflicts, use global using alias to resolve it.

Global usings keep DodaTech codebases clean and reduce merge conflicts from repeated using statements. For more, visit DodaTech.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro