1

In the past I have avoided defining nested classes. But if a class is only ever going to be used inside the primary-class, then is nesting the class the ideal place for it? Are there any accepted conventions on this subject?

public class PrimaryClass
{
    public class SubClass
    {
        // ...
    }

    public SubClass MySubClass { get; set; }
    // ...
}

Or should it always be

public class SubClass
{
    // ...
}

public class PrimaryClass
{
    public SubClass MySubClass { get; set; }
    // ...
}
Luke Baulch
  • 3,626
  • 6
  • 36
  • 44

4 Answers4

1

You are exactly correct- if the sub class is only ever used in the main class, it is good practice to separate the concerns. In Java, this is a language level construct called anonymous inner classes. You can always refactor the subclass out later if you need to.

The wiki article comparing C# and Java has a pretty good analysis, covering both the general guidelines and the language specific concerns. http://en.wikipedia.org/wiki/Comparison_of_C_Sharp_and_Java#Inner_and_local_classes

David Souther
  • 8,125
  • 2
  • 36
  • 53
  • 1
    They are not anonymous if they are named as in this example, but instead just "inner" classes. An anonymous class is one that does not contain the "class Name" declaration. Also, if marked public this class is accessible from outside the surrounding class as well. – filip-fku Aug 22 '11 at 04:50
  • @filip-fku Correct. As I mentioned, in Java, the anonymous inner class is a *similar* construct. There are several important, subtle differences, especially with regards to accessing static variables. – David Souther Aug 22 '11 at 04:55
  • But there is a closer match in Java to what is demonstrated here than an "anonymous inner" class - simply a named inner/nested class. I think it would be more helpful if you refer to "inner class" instead of "anonymous inner class" in your comparison to Java concepts. An anonymous class serves as both class declaration and instantiation in Java, which is not the case here. – filip-fku Aug 22 '11 at 05:00
0

If the inner class is an intrinsic part of its parent outer class, then it's perfectly fine to do so. I see an inner class just like any other member of the outer class, except it comes with its own set of methods and properties.

For example, an engine is an intrinsic part of a motor vehicle. It cannot function outside one (hence meaningless), so in other words, the meaningfulness of its existence depends solely on the motor vehicle. I am of course assuming here that an engine cannot exist in anything else but a motor vehicle (can't think of a better example, sorry).

in C#, you can of course also implement the engine as a separate class and refer to it from within the motor vehicle class. It's really a choice on your part.

deutschZuid
  • 1,028
  • 2
  • 15
  • 33
0

In addition to using the nested class solely inside the outer class, there are cases where you want to hand instances of it to the outside world. Typically you use inerfaces to do that. Here's an example of how you might use nested classes and an interface:

   public interface IDoSomething {...}

   public class Outer
   {

        public static IDoSomething GetSomethingToDo( ... parameters ...)
        { 
            if (...) return new SomethingA();
            else return new SomethingB();
        }

        private class SomethingA : IDoSomething { ...}
        private class SomethingB : IDoSomething { ...}

    }

The implementation of DoSomething is completely hidden from the outside world.

Ian Mercer
  • 38,490
  • 8
  • 97
  • 133
0

I tend to define nested classes only as private, never public. The nested class, in this way, will be used only by the enclosing class. Only when the nested class is used by more than one top-level class do I move the class to top level. Then I usually make it internal.

Using public nested classes should be reserved for enumerations.

Peter O.
  • 32,158
  • 14
  • 82
  • 96