0

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 namespace?

Artur A
  • 7,115
  • 57
  • 60
  • Why do you have nested classes? – Lasse V. Karlsen Apr 01 '11 at 07:26
  • 1
    When you do have nested classes they are in the same file as its enclosing class, unless if they reside in partial classes. What do you mean? – Mikael Östberg Apr 01 '11 at 07:29
  • Yes, there are several nested classes in the partial class. Now I can see that it is because of bad architecture. Nevertheless, there can be such situation and with much code, so I think it would be better to move these classes in seperate files. – Artur A Apr 01 '11 at 08:00

2 Answers2

0

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.

Florian Greinacher
  • 14,478
  • 1
  • 35
  • 53
  • Thanks. It is a good idea. There is a [link](http://stackoverflow.com/questions/1478610/naming-conventions-for-partial-class-files) with the hint for visualizing with in project properties. – Artur A Apr 01 '11 at 09:22
  • Yes, I use that too. Too bad you cannot do this directly in Visual Studio. – Florian Greinacher Apr 01 '11 at 09:24
0

Given your example, personally I would name them accordingly:

  • Class1.cs
  • Class1.NestedClass1.cs
  • Class1.NestedClass2.cs
  • Class2.cs
MattDavey
  • 8,897
  • 3
  • 31
  • 54