Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested Partial Class

Tags:

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?

like image 307
TrueGrime Avatar asked Feb 04 '12 17:02

TrueGrime


People also ask

What are the four types of nested classes?

As a member of the OuterClass , a nested class can be declared private , public , protected , or package private.

What is partial class example?

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.

What are the two types of nested classes?

There are two types of nested classes non-static and static nested classes. The non-static nested classes are also known as inner classes.

Can a partial class inherit?

Inheritance cannot be applied to partial classes.


2 Answers

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      {     } } 
like image 160
Dmitry Polyanitsa Avatar answered Oct 14 '22 21:10

Dmitry Polyanitsa


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.

like image 25
user941998 Avatar answered Oct 14 '22 19:10

user941998