开发者

naming folders in nested classes

If we have the following situation:

namespace SomeNameSpace
{
    public class Class1
    {
        private class NestedClass1 
        {
            // NestedClass1 implementation
        }

        private class NestedClass2 
        {
            // NestedClass1 implementation
        }
    }

    public class Class2
    {
        // so on ...
    }
}

What is the best way to organize folders and files for nested classes?

First of all, we have Folder "SomeNameSpace". Then files "Class1.cs, Class2.cs, ...".

Consider nestes classes in "Class1". Is it the right way to create files "Class1.NestedClass1.cs, ..." and place them in folder SomeNameSpace?

Or create new folder, then what name should it have, and may be it is not proper way because it looks like new nam开发者_运维知识库espace?


In general nested classes are somehow considered bad practice, but sometimes you do need them. Implementing a state pattern often is much easier if the states are nested classes that can access private methods of their context.

I tend to create separate files like Class.Subclass.cs if I have more than one nested class or one nested class that clutters the code of its parent class. In this case of course Class has to be declared partial.

Nevertheless this is just my personal opinion, do it as you/your team like it best.


Given your example, personally I would name them accordingly:

  • Class1.cs
  • Class1.NestedClass1.cs
  • Class1.NestedClass2.cs
  • Class2.cs
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜