Should 'using' directives be inside or outside the namespace?

前端 未结 12 1367
长情又很酷
长情又很酷 2020-11-21 22:41

I have been running StyleCop over some C# code, and it keeps reporting that my using directives should be inside the namespace.

Is there a technical rea

12条回答
  •  清酒与你
    2020-11-21 23:05

    The technical reasons are discussed in the answers and I think that it comes to the personal preferences in the end since the difference is not that big and there are tradeoffs for both of them. Visual Studio's default template for creating .cs files use using directives outside of namespaces e.g.

    One can adjust stylecop to check using directives outside of namespaces through adding stylecop.json file in the root of the project file with the following:

    {
      "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json",
        "orderingRules": {
          "usingDirectivesPlacement": "outsideNamespace"
        }
      }
    }
    

    You can create this config file in solution level and add it to your projects as 'Existing Link File' to share the config across all of your projects too.

提交回复
热议问题