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?
As a member of the OuterClass , a nested class can be declared private , public , protected , or package private.
A partial class is a special feature of C#. It provides a special ability to implement the functionality of a single class into multiple files and all these files are combined into a single class file when the application is compiled. A partial class is created by using a partial keyword.
There are two types of nested classes non-static and static nested classes. The non-static nested classes are also known as inner classes.
Inheritance cannot be applied to partial classes.
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.
partial class c1 { partial class c2 { class c3 { } } }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With