- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# - Switch Expressions
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# - Foreach Loop
- C# - Goto Statement
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Custom Exceptions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - LINQ
- C# - IEnumerable vs IEnumerator
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Tasks and Parallel Programming
- C# - Multithreading
- C# - Extension Methods
- C# - Lambda Expressions
- C# - Async and Await
- C# Modern Features
- C# - Tuples
- C# - Records
- C# - Pattern Matching Enhancements
- C# - Top-level Statements
- C# - Nullable Reference Types
- C# - What's New in C# 11 / 12 / 13
- C# - Global Usings
- C# - File-Scoped Namespaces
- C# Practical & Advanced Usage
- C# - JSON & XML Handling
- C# - Data Serialization & Deserialization
- C# - REST API Calls with Httpclient
- C# - Dependency Injection
- C# - Unit Testing with NUnit, xUnit & MSTest
- C# - Package Management with NuGet
Global Usings in C#
Global Using is a way to write a namespace once and use it in all files of a project. It was introduced in C# 10. Normally we write using statements at the top of each file to access classes like Console but with global using we write it once and it becomes available in the whole project.
We define global using statements in a separate file named GlobalUsings.cs, which we create manually and any namespace we add there becomes automatically available in every file of the project.
Note that if a namespace is declared both locally in a file and globally, the local declaration will be used in that file, and the global one will be ignored for that specific file.
Syntax of Global Using
To declare a global using, we write the global keyword before using, followed by the namespace name −
global using System;
We can also declare multiple namespaces in the same file like this −
global using System; global using System.Collections.Generic; global using System.Linq;
Example with Normal Using
In this example, we have two files that both use the System namespace. We write using System; statement at the top of each file so we can access classes like Console. For that each file requires its own using statement.
File 1: Program1.cs
using System;
class Program1
{
static void Main()
{
Console.WriteLine("Hello from Program1");
}
}
File 2: Program2.cs
using System;
class Program2
{
static void Main()
{
Console.WriteLine("Hello from Program2");
}
}
Here, both files have the same using System; statement to use the Console class, so the namespace is repeated in multiple places.
Output when running Program1 -
Hello from Program1
Output when running Program2 -
Hello from Program2
Example with Global Using
Here, we declare the System namespace globally in a separate file called GlobalUsings.cs. This makes it available in all files of the project. We do not need to write using System; in each file.
GlobalUsings.cs
global using System;
File 1: Program1.cs
class Program1
{
static void Main()
{
Console.WriteLine("Hello from Program1");
}
}
File 2: Program2.cs
class Program2
{
static void Main()
{
Console.WriteLine("Hello from Program2");
}
}
Now, both Program1 and Program2 can use the Console class without having their own using statements.
Output when running Program1 −
Hello from Program1
Output when running Program2 −
Hello from Program2
Benefits of Global Using
Following are the main benefits of using global usings in our projects -
- It reduces the repeated declaration of namespaces across multiple files and keeps the program organized.
- We can use it in large projects where many files share the same namespaces to avoid confusion or repetition.
- Adding or removing a namespace globally requires changes in only one place.
- It also ensures that all files automatically have access to the same namespaces.
How to Create Global Usings
Let's see how we can create global usings and use them in our project. By following these steps, we can set them up once and have the namespaces available in all files.
Step 1: Create a New File
First, we create a new C# file in our project. We can name it anything but the convention is to call it GlobalUsings.cs or Usings.cs.
To do this, right-click on your project -> Add -> New Item -> C# Class, and then give it a name, for example, GlobalUsings.cs.
Step 2: Add the Global Keyword
In this file, we write our using statements, but we add the global keyword before each one. For example −
global using System; global using System.Collections.Generic; global using System.Linq;
Now these namespaces are available in every file in our project automatically.
Step 3: Example
For example, we will create a calculator. Here, we do not write using statements at the top of the file, but we can still use List<int> and LINQ methods like .Average() because of the global using statements in GlobalUsings.cs.
Calculator.cs
// Calculator.cs
// Notice: NO using statements at the top!
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
public int Subtract(int a, int b)
{
return a - b;
}
public double Average(List<int> numbers)
{
return numbers.Average();
}
}
We will call the calculator program from another file (Program.cs). Here again, we can use Console class and List<int> without importing namespaces because they are declared globally.
Program.cs
// Program.cs
// Notice: NO using statements at the top!
public class Program
{
public static void Main()
{
// We can use Console because of global using System
Console.WriteLine("=== Calculator Application ===");
var calc = new Calculator();
int sum = calc.Add(10, 5);
Console.WriteLine($"10 + 5 = {sum}");
int difference = calc.Subtract(10, 5);
Console.WriteLine($"10 - 5 = {difference}");
var numbers = new List<int> { 10, 20, 30, 40, 50 };
double average = calc.Average(numbers);
Console.WriteLine($"Average of numbers: {average}");
}
}
Following is the output of the program.
=== Calculator Application === 10 + 5 = 15 10 - 5 = 5 Average of numbers: 30
Conclusion
In this chapter, we learned about Global Using in C#, a feature added in C# 10. It lets us declare namespaces once and use them anywhere in our project. This makes our code cleaner, easier to read, and much simpler to manage especially when working on large projects.