"Public" nested classes or not

You might want to check out what Microsoft has to say on the topic. Basically it's a question of style I'd say.


You can use namespaces to relate things that are... related.

For example:

namespace Diner
{
    public class Sandwich
    {
        public Sandwich(Filling filling) { }
    }

    public class Filling { }
}

The advantage of this over using classes as if they were namespaces is that you can optionally use using on the calling side to abbreviate things:

using Diner;

...

var sandwich = new Sandwich(new Filling());

If you use the Sandwich class as if it were a namespace for Filling, you have to use the full name Sandwich.Filling to refer to Filling.

And how are you going to sleep at night knowing that?


I think it's fine. This is basically the builder pattern, and using nested classes works pretty well. It also lets the builder access private members of the outer class, which can be very useful. For instance, you can have a Build method on the builder which calls a private constructor on the outer class which takes an instance of the builder:

public class Outer
{
    private Outer(Builder builder)
    {
        // Copy stuff
    }

    public class Builder
    {
        public Outer Build()
        {
            return new Outer(this);
        }
    }
}

That ensures that the only way of building an instance of the outer class is via the builder.

I use a pattern very much like this in my C# port of Protocol Buffers.