In C# 10, a new feature was introduced that allows you to specify global using directives. This feature lets you define using directives that apply to all the files in your project, reducing the need for repetitive using statements in each file. This can be particularly useful for commonly used namespaces.
How to Specify Global Using Directives in Code
To specify a global using directive, you place the global modifier in front of the using directive in a .cs file. It’s common practice to place these global using directives in a dedicated file, such as GlobalUsings.cs, for better organization.
Here’s an example of how you can define and use global using directives:
Create a Global Usings File: Create a new file in your project, for
// GlobalUsings.cs
global using System;
global using System.Collections.Generic;
global using System.Linq;
global using System.Threading.Tasks;
Remove Redundant Usings: After defining the global using directives, you can remove the corresponding using statements from your other files since these namespaces are now available globally.
// ExampleFile.cs
using System.IO; // You can still use file-specific usings
public class Example
{
public void DoSomething()
{
Console.WriteLine("Hello, World!"); // No need for using System;
List<int> numbers = new List<int> { 1, 2, 3 }; // No need for using System.Collections.Generic;
}
}
Benefits
- Cleaner Code: Reduces clutter by eliminating repetitive using directives across multiple files.
- Centralized Management: Easier to manage and update common namespaces from a single location.
Usage Considerations
- Scope: The global using directive applies to the entire project, so be mindful of potential namespace conflicts.
- Maintenance: Keep the list of global usings manageable to avoid unnecessary inclusions, which could lead to increased compile times or unintended dependencies.
Example Project Structure
codeMyProject/
│
├── GlobalUsings.cs
├── Program.cs
├── ExampleFile.cs
│
└── ... (other files and folders)
How to Specify Global Using Directives in .csproj
You can also define global using directives within the .csproj file using the <Using> element inside an <ItemGroup>. This provides the same behavior as doing it in the code file, and removes the clutter of an extra file or global using commands. But the behavior is more hidden in the .csproj file rather than visible in your project.
Here’s an example:
Open your project’s .csproj file and add the global using directives under an <ItemGroup>.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Using Include="System" />
<Using Include="System.Collections.Generic" />
<Using Include="System.Linq" />
<Using Include="System.Threading.Tasks" />
</ItemGroup>
</Project>
The same usage code above in Example.cs should also run with using commands applied to the .csproj file.
Conclusion
Global using directives in C# 10 are a powerful feature that can help you streamline your code and improve maintainability. By placing common using directives in a single file, you can reduce redundancy and ensure consistent namespace usage across your project.