views:

78

answers:

3

Hello, Is there a way in C# to tidy up the following class declaration?

namespace Application.Namespace
{
    public class MasterClass
    {
        public class FlyingSaucer
        {
            public class Rotator
            {
                public class Cube
                {

Still maintaining the class structure, just cleaning up the declaration.

+2  A: 

No - it's already pretty tidy, given that you've got 4 levels of nesting.

But you almost certainly shouldn't have 4 levels of nesting to start with. Why would you want to do that? You've got a class named subClass1 which isn't a subclass by the normal meaning (i.e. derived from another specific class; subClass1 is only derived from object.)

Nesting classes is fairly rare - I don't think I've ever seen even three levels of nesting, let alone four. Why do you think this is a good idea?

Jon Skeet
thanks for the reply Jon. Don't worry about the class names themselves, this was just written to illustrate my point. As for why we have four levels of nesting .... ummmm good question!
Guamez
Why would nesting to four levels be seen as "bad practice"?
Guamez
@Guamez. When nested classes are public, as they are here, it's syntactically awkward for the calling code, as the full path through from the outermost class must be used, as one cannot declare it as one can a namespace (that is a "namespace" in the .NET sense of the word, outer class names are a "namespace" in the general sense too). It is also a bad smell as you will quite possibly end up forcing someone to think in terms of the outer class even when that's not their concern at the point in time. With private classes its less awkward and I've certainly gone at least 3 levels in the past...
Jon Hanna
... but in those cases the inner classes existed purely to offer something to the class one step further out and either didn't escape that class, or did so only by implementing a publicly defined interface (in some cases a private implementation of `IEnumerable<T>` as an inner class that has a private implementation of `IEnumerator<T>` within it, can be quite neat).
Jon Hanna
@Guamez: You should normally default to *not* nesting classes... you should normally just make classes top-level in the namespace unless you have a good reason.
Jon Skeet
A: 

it looks like you are confusing inheritance with sub classes.

sub classing is usually fairly rare.

John Nicholas
+1  A: 

Are there any relationship between

FlyingSaucer -- >Rotator -- >Cube , i am thinking about it is a typical example of Containment.

FlyingSaucer Contains Rotator and Rotator Contains Cube.

so if that is the case , you may consider using Composition by creating properties of Rotator and Cube in FlyingSaucer.

You can inject these dependencies while creating an object FlyingSaucer. This will remove the very deep nesting in your classes and i think , you probably need to access objects of FlyingSaucer , Rotator , Cube from your master class.

so use Composition here it will really make your class hierarchy simpler and easy to extend and maintain.

saurabh