Nested Partial Class

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 
    {
    }
}

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.

Tags:

C#