Namespace and class with the same name?

前端 未结 9 1511
终归单人心
终归单人心 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:07

    Even though I agree with other answers in that you should not name your class the same as your namespace there are times in which you cannot comply with such requirements.

    In my case for example I was not the person making such a decision therefore I needed to find a way to make it work.

    So for those who cannot change namespace name nor class name here is a way in which you can make your code work.

    // Foo.DLL: 
    namespace Foo { public class Foo { } }
    
    // Bar.DLL: 
    namespace Bar { public class Foo { } }
    
    // Blah.DLL: 
    namespace Blah
    {
        using FooNSAlias = Foo;//alias
        using BarNSAlias = Bar;//alias
        class C { FooNSAlias.Foo foo; }//use alias to fully qualify class name
    }
    

    Basically I created namespace "aliases" and that allowed me to fully qualify the class and the Visual Studio "confusion" went away.

    NOTE: You should avoid this naming conflict if it in your control to do so. You should only use the mentioned technique when you are not in control of the classes and namespaces in question.

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

    Just Adding my 2 cents:

    I had the following class:

    namespace Foo {
        public struct Bar {
        }
        public class Foo {
            //no method or member named "Bar"
        }
    }
    

    The client was written like this:

    using Foo;
    
    public class Blah {
        public void GetFoo( out Foo.Bar[] barArray ) {
        }
    }
    

    Forgiving the error GetFoo not returning the output instead of using the out parameter, the compiler could not resolve the data type Foo.Bar[] . It was returning the error: could not find type or namespace Foo.Bar .

    It appears that when it tries to compile it resolved Foo as the class and did not find an embedded class Bar in the class Foo. It also could not find a namespace called Foo.Bar . It failed to look for a class Bar in the namespace Foo. The dots in a name space are NOT syntactic. The whole string is a token, not the words delimited by the dots.

    This behaviour was exhibited by VS 2015 running .Net 4.6

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

    Old post, but here I go with another idea that may help someone:

    "...but it seems the only way to do that would be to make all the other classes inner classes of Scenegraph in the Scenegraph.cs file and that's just too unwieldy."

    This is really the better implementation for a bunch of scenarios. But, I do agree that having all that code on the same .cs file is annoying (to say the least).

    You could solve it by making the base class a "partial class" and then, go on creating the inner classes on their own files (just remember that they'll have to declare the base class complement and then go on with the specific inner class for that file).

    Something like...

    Scenegraph.cs:

    namespace MyLib
    {
        public partial class Scenegraph
        {
            //Scenegraph specific implementations
        }
    }
    

    DependentClass.cs:

    namespace MyLib
    {
        public partial class Scenegraph
        {
            public class DependentClass
            {
                //DependentClass specific implementations
            }
        }
    }
    

    I do think that this is the closer that you can get to having the clean implementation of inner classes while not having to clutter everything inside one huge and messy file.

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

    I don't recommend you to name a class like its namespace, see this article.

    The Framework Design Guidelines say in section 3.4 “do not use the same name for a namespace and a type in that namespace”. That is:

    namespace MyContainers.List 
    { 
        public class List { … } 
    }
    

    Why is this badness? Oh, let me count the ways.

    You can get yourself into situations where you think you are referring to one thing but in fact are referring to something else. Suppose you end up in this unfortunate situation: you are writing Blah.DLL and importing Foo.DLL and Bar.DLL, which, unfortunately, both have a type called Foo:

    // Foo.DLL: 
    namespace Foo { public class Foo { } }
    
    // Bar.DLL: 
    namespace Bar { public class Foo { } }
    
    // Blah.DLL: 
    namespace Blah  
    {   
    using Foo;   
    using Bar;   
    class C { Foo foo; } 
    }
    

    The compiler gives an error. “Foo” is ambiguous between Foo.Foo and Bar.Foo. Bummer. I guess I’ll fix that by fully qualifying the name:

       class C { Foo.Foo foo; } 
    

    This now gives the ambiguity error “Foo in Foo.Foo is ambiguous between Foo.Foo and Bar.Foo”. We still don’t know what the first Foo refers to, and until we can figure that out, we don’t even bother to try to figure out what the second one refers to.

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

    As others have said, it's a good practice to avoid naming a class the same as its namespace.

    Here are some additional naming suggestions from an answer by svick to a related question "Same class and namespace name" on the Software Engineering Stack Exchange:

    You're right that you shouldn't name the namespace the same as a type it contains. I think there are several approaches you can use:

    • Pluralize: Model.DataSources.DataSource

    This works especially well if the primary purpose of the namespace is to contain types that inherit from the same base type or implement the same interface.

    • Shorten: Model.QueryStorage

    If a namespace contains only a small number of types, maybe you don't need that namespace at all.

    • Make enterprisey: Model.ProjectSystem.Project

    This can work especially for features that are important part of your product, so they deserve their own name.

    (Note that the above answer uses Model.DataSource.DataSource, Model.QueryStorage.QueryStorage., and Model.Project.Project as examples rather than MyLib.Scenegraph.Scenegraph.)

    (I also found the other naming suggestions in the other answers here helpful.)

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

    I would suggest that you follow the advice I got on microsoft.public.dotnet.languages.csharp to use MyLib.ScenegraphUtil.Scenegraph and MyLib.ScenegraphUtil.*.

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