Namespace and class with the same name?

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

提交回复
热议问题