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

前端 未结 12 1407
长情又很酷
长情又很酷 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 22:49

    One wrinkle I ran into (that isn't covered in other answers):

    Suppose you have these namespaces:

    • Something.Other
    • Parent.Something.Other

    When you use using Something.Other outside of a namespace Parent, it refers to the first one (Something.Other).

    However if you use it inside of that namespace declaration, it refers to the second one (Parent.Something.Other)!

    There is a simple solution: add the "global::" prefix: docs

    namespace Parent
    {
       using global::Something.Other;
       // etc
    }
    

提交回复
热议问题