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:
- 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?
- Is it possible to add my own global usings?
- Is it possible to remove some implicitly included global usings (e.g. in case of name clashes)?
- 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.
Yes, they are just for convenience when working with namespaces (reduce clutter, create global aliases, easier namespace management overall)
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:Yes, you can remove automatically imported namespaces (from the project default imports with
ImplicitUsings) withNo AFAIK, except for mentioned earlier.
Useful links:
UsingMSBuild propglobalmodifier language reference