Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I control what usings are made global in a C#10 / .NET6.0 project?

C#10 in .NET 6.0 supports a new feature called global using directive.

It seems to do 2 things:

  • When you have a namespace in the global using, you don't have to include the using ... for that namespace at the top of your *.cs files.
  • Some namespaces are implicitly added to the global usings, which ones depend on the project type it seems.

I know I can disable the implicit adding of namespaces to the global usings by adding the following to my project file:

<PropertyGroup>
    <ImplicitUsings>disable</ImplicitUsings>
</PropertyGroup>

My question:

  1. Is this global using directive feature just for convenience so that some often used namespaces are included globally, so these usings don't clutter the top of your *.cs files?
  2. Is it possible to add my own global usings?
  3. Is it possible to remove some implicitly included global usings (e.g. in case of name clashes)?
  4. Does this this global using directive feature has other uses I did not think off?

The global-usings file states // <auto-generated/> at the top so I cannot just add/remove/edit global-usings in this file.

like image 487
Stefan Avatar asked Dec 31 '25 06:12

Stefan


2 Answers

Is this global using directive feature just for convenience so that some often used namespaces are included globally, so these usings don't clutter the top of your *.cs files?

Yes, they are just for convenience when working with namespaces (reduce clutter, create global aliases, easier namespace management overall)

Is it possible to add my own global usings?

Yes, you can add global usings to the project either by adding global using Some.Namespace; to the top of one of the files (you can create a separate one specially for that) or by adding the following to the .cproj:

<ItemGroup>
    <Using Include="Some.Namespace"/>
</ItemGroup>

Is it possible to remove some implicitly included global usings (e.g. in case of name clashes)?

Yes, you can remove automatically imported namespaces (from the project default imports with ImplicitUsings) with

<ItemGroup>
    <Using Remove="Some.Namespace"/>
</ItemGroup>

Does this this global using directive feature has other uses I did not think off?

No AFAIK, except for mentioned earlier.

Useful links:

  • Where are the using statements/directives in .NET 6 answer
  • Implicit using directives per SDK used
  • Using MSBuild prop
  • Global Using Directive feature specification
  • global modifier language reference
like image 161
Guru Stron Avatar answered Jan 02 '26 20:01

Guru Stron


  1. Mostly yes. You can move your classes from one globally used namespace to another without changing the source code. Which massively saves time during refactoring.
  2. Yes. Instead of using namespace_name declare global using namespace_name that's it! This declaration can be anywhere in the project. Ideally, create an import.cs file and declare all your globally used namespaces there.
  3. Yes. Find where it is declared as global using nameclashing_namespace and remove the global word.
  4. I can't think of any apart from (1) above.
like image 32
Mayur Ekbote Avatar answered Jan 02 '26 20:01

Mayur Ekbote



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!