21
internal partial class Class1
{
    private class Class2 : ISomething, ISomethingElse
    {
        private class Class3 : ISomething
        {

        }
    }
}

I want Class3 itself to host another private Class4, but that would make Class3 too big so I want to make Class3 partial. I know how to do that to a non-nested class. How would you make a nested class partial in a separate file?

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
TrueGrime
  • 275
  • 1
  • 2
  • 8
  • 13
    You may wish to reconsider your design if you need 4 levels of nested classes. – Dan Bryant Feb 04 '12 at 18:02
  • While nested classes can be useful (http://stackoverflow.com/questions/1083032/why-would-i-ever-need-to-use-c-sharp-nested-classes) I would still kindly suggest considering another approach to whatever you are building, here. – summea Feb 04 '12 at 18:02
  • yes, please refactor - this doesn't sound like a good idea at all – BrokenGlass Feb 04 '12 at 18:03
  • 1
    It might seems like a bad design, but why expose Class2 for everyone if it is only required and designed for Class1? same applies to Class3 and Class4. I only expose classes that might be used by more than 1 class, otherwise I nest. – TrueGrime Feb 04 '12 at 18:08
  • If your classes are big and functional enough, you might just split them into separate libraries, and so nested classes can become first level classes with internal visibility. – Dmitry Polyanitsa Feb 04 '12 at 18:11
  • 2
    I still cant see whats wrong with 4 levels of nested private classes. I mean you all said not to do it, but I cant see a compelling reason why not. I know 100% that all the nested classes wont be used anywhere else other than their host class. – TrueGrime Feb 04 '12 at 18:21
  • There's little point in using private classes when the outer class is internal. Just make them internal. – Hans Passant Feb 04 '12 at 18:30
  • 1
    But I also want to hide them from classes within the same assembly or namespace. Because why not if I can keep things clean and nicely divided for myself? – TrueGrime Feb 04 '12 at 18:32
  • 1
    Be keeping the code clean and in nice little understandable blocks you save yourself from the awful pain in the neck in the future. – Dmitry Polyanitsa Feb 04 '12 at 18:35
  • 1
    This pattern also has a practical value for generated classes. Some tools produce several levels of nested partial classes (e.g. linq2xsd). – Puterdo Borato Apr 28 '15 at 08:15
  • The purpose of partial classes is to allow you to extend auto-generated classes without tampering with the generated code; it isn't simply to split large classes into several files. Here is a more realistic scenario: a code generator such as Visual Studio's DataSet designer generates nested classes, and you want to extend the nested classes in your own code. – Ron Inbar Jun 23 '17 at 23:46

2 Answers2

26

This article states that it's possible to make nested classes partial even if their parent class is not partial. But then you can't separate them in several files, so I think you need to make Class2 partial too and split just like you would with first-level classes, keeping the partial class hierarchy.

I really hope that this question is just because of curiosity.

EDIT: Just tried this - works ok.

file1.cs

partial class c1 
{
    partial class c2 
    {
        class c3 
        {
        }
    }
}

file2.cs

partial class c1 
{
    partial class c2 
    {
    }
}
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Dmitry Polyanitsa
  • 1,083
  • 9
  • 18
  • So basically even if I make all classes in the hierarchy partial, I still cannot split any of them into separate files except for the top-most class? – TrueGrime Feb 04 '12 at 18:17
  • I probably explained it wrong. From the article I've got that you can split the way you want, you only need to keep the hierarchy in the file in which you want to put the Class4. You can just try it out and see if I got it right. – Dmitry Polyanitsa Feb 04 '12 at 18:27
  • If the top most class is not partial, it cannot be spilt into serveral files. So how could the nested classes be split into serveral files, since they have to reside inside the top most class? On the other side, if the top most class can be spilt into serveral files, then the nested partial classes can be split into several files as well. – Olivier Jacot-Descombes Feb 04 '12 at 18:29
  • The class Class1 is partial (in his question), Class3 he needs to split (so he will make it partial) and I have mentioned in my answer that he will need to make Class2 partial, too. – Dmitry Polyanitsa Feb 04 '12 at 18:31
2

If a nested class needs to be for any reason partitioned then also the owner class needs to be partitioned. Nested class code IS also the owner class code - it is a composition relation.

Thus extracting part of a nested class means also extracting part of its owner at the same time. In turn we need to "see full path" to each partial class so that compiler can identify the fully specified type.

It is the same with namespaces - unlike classes they are somehow partial implicitly, because we do not expect having whole namespace in the same file, while we normally do so for classes, especially the nested ones.

So normally we need to write in a file with parts of our nested classes

MyTopNamespace.MyLevel2Namespace.MyTopLevelClass.MyNestedClassA
MyTopNamespace.MyLevel2Namespace.MyTopLevelClass.MyNestedClassB

defined something like:

namespace MyTopNamespace 
{ 
    namespace MyLevel2Namespace 
    {
        partial class MyTopLevelClass
        {
            partial class MyNestedClassA
            {
                // Part of definition for our nested class:
                // MyTopNamespace.MyLevel2Namespace.MyTopLevelClass.MyNestedClassA
            }
            class MyOtherNestedClassNotPartitioned
            {
               ...
            }
            partial class MyNestedClassB
            {
                // Part of definition for our nested class:
                // MyTopNamespace.MyLevel2Namespace.MyTopLevelClass.MyNestedClassB
            }
        }
    }
}

and in another file with other part of nested class suffixed 'A' defined something like:

namespace MyTopNamespace 
{ 
    namespace MyLevel2Namespace 
    {
        partial class MyTopLevelClass
        {
            partial class MyNestedClassA
            {
               // Another part of definition for our nested class:
               // MyTopNamespace.MyLevel2Namespace.MyTopLevelClass.MyNestedClassA
            }
       }
    }
}

and in yet another file with other part of nested class suffixed 'B' defined something like:

namespace MyTopNamespace 
{ 
    namespace MyLevel2Namespace 
    {
        partial class MyTopLevelClass
        {
            partial class MyNestedClassB
            {
               // Another part of definition for our nested class:
               // MyTopNamespace.MyLevel2Namespace.MyTopLevelClass.MyNestedClassB
            }
       }
    }
}

Or we can have other files with parts of both nested classes defined etc., but we always need to fully specify where is the class type defined.

user941998
  • 21
  • 2