File name convention for C# v10 global using declarations
Asked Answered
D

1

8

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.

Dowzall answered 9/12, 2021 at 19:2 Comment(3)
It's a new feature so there's not enough time for a consensus, or even deciding if one is needed. All you'll get is a lot of opinions. Why not just Globals.cs for example?Giovannagiovanni
I see a few posts advocating for Usings.cs. For example: youtube.com/watch?v=RWYrafpP53AAthenaathenaeum
I try to name it evrywhere _GlobalUsings.cs with a leading underscore, so I have it on the top of the list in Solution Explorer and not somewhere in the middle of my regular classes. The leading underscore is, like you mentioned, similar to razor/blazor. I don't get it why Visual Studio names it "GlobalUsings.cs" for new projects. Sadly that is not configurable.Tom
I
7

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

Isotherm answered 29/12, 2021 at 13:34 Comment(3)
I have been putting my global using statements in file GlobalUsings.cs with success. My original concern about a clash with the toolchain auto-generated GlobalUsings.g.cs file was unfounded. At the end of the day neither of these files contain type declarations, just bundles of using statements.Dowzall
Awesome @camelCase! I did the same thing just yesterday. My code seems a tiny bit cleaner and easier to read now. I'm sure I will notice it more when I go back to older projects.Isotherm
The blog recommends all lowercase globalusings.cs, but I can't bring myself to do that. It looks so odd with literally every other file being PascalCase.Isotherm

© 2022 - 2024 — McMap. All rights reserved.