Namespace and class with the same name?

前端 未结 9 1512
终归单人心
终归单人心 2020-11-29 03:38

I\'m organizing a library project and I have a central manager class named Scenegraph and a whole bunch of other classes that live in the Scenegraph namespace.<

相关标签:
9条回答
  • 2020-11-29 04:23

    It happens when it's the main class of the namespace. So it's one motivation to put the namespace in a library, then the issue goes away if you add 'Lib' to the namespace name...

    namespace SocketLib
    {
        class Socket
        {
    
    0 讨论(0)
  • 2020-11-29 04:28

    Giving the same name to the namespace and the class can confuse the compiler as others have said.

    How to name it then?

    If the namespace has multiple classes then find a name that defines all those classes.

    If the namespace has just one class (and hence the temptation to give it the same name) name the namespace ClassNameNS. This is how Microsoft names their namespaces at least.

    0 讨论(0)
  • 2020-11-29 04:33

    CA1724: Type Names Should Not Match Namespaces ...

    Basically, if you follow Code Analysis for proper coding this rule says to not do what you are trying to do. Code Analysis is very useful in helping you find potential issues.

    0 讨论(0)
提交回复
热议问题