naming-conventionsusing-directivesc#-10.0

File name convention for C# v10 global using declarations


Is there a consensus within the C# development community on the .cs filename in which global using statements are declared?

I was going to adopt the filename GlobalUsings.cs but then found that a hidden file called MyProject.GlobalUsings.g.cs is created behind the scenes by the VS2022 toolchain. This is to support the related new C# 10 feature called Implicit global using directives.

Blazor has supported a similar feature for .razor files and the Blazor solution template automatically creates a file called _Imports.razor. That name is derived from the Razor syntax to declare a using reference.


Solution

  • Short Answer

    Usings.cs or maybe GlobalUsings.cs.

    More Info

    There are actually 2 new features. They seem really simple at first, but the more you read about it, the more complicated it becomes.

    1. Global using directives. You can use global in front of any using directive in any file to make it global in the project.
    2. Implicit usings. This automatically adds a set of common global using directives depending on the project type. You can enable this in a project that is upgraded to .NET 6 buy putting this in the project csproj file: <PropertyGroup><ImplicitUsings>enable</ImplicitUsings>...

    Implicit usings is enabled by default on new .NET 6 projects, so it sounds like the convention is:

    1. Use implicit usings.
    2. You may still need a file to store global usings that are not implicit. I like your idea of GlobalUsings.cs. It's self-documenting.

    In fact, this naming is recommended by the Welcome to C# 10 Blog. I highly recommend reading it; it was really helpful to me.

    EDIT:

    This seems to keep changing. .NET 6 project templates are now including a Usings.cs file. enter image description here