1

Is it possible in C# to define a private class without nesting in a parent class? Below is a simplified example of what I'm trying to do

public abstract class ClassA<T> 
{
    public T Value { get; set; }

    public ClassA(T value)
    {
        Value = value;
    }
}

private class ClassB : ClassA<int>
{
    public ClassB(int value) 
        : base(value)
    {
    }
}

I'd like for ClassB to only be accessible by ClassA. I'm wondering if what I'm trying to do is possible if the two classes are in the same file. Basically, it would be nice to hide extensions of ClassA, but I'd rather not nest too many classes.

rookie
  • 2,783
  • 5
  • 29
  • 43
  • 1
    There is something fundamentally wrong here, why would you want a parent class to know anything about its child classes? The point of abstraction is to delegate responsibility for implementation to the child classes so that the parent class does not have to define or know about the implementation. Do you mean you only want child classes to know about parent classes? Marking abstract means nobody can make an instance of ClassA anyway... – Ron Beyer Jul 07 '15 at 16:03
  • @RonBeyer, it all goes wrong at the point where `abstract` is used instead of `sealed`... ;) – David Arno Jul 07 '15 at 16:06
  • @RonBeyer I don't really like nesting something as dense as a class -- I can see where it makes sense, though, in this particular instance. – rookie Jul 07 '15 at 16:56

2 Answers2

6

What would be the purpose of having a private class on its own? you won't be able to reference that class. private classes are only possible inside another classes because the outer class can access it.

Francisco Goldenstein
  • 13,299
  • 7
  • 58
  • 74
  • That makes sense. I'm guessing there's no other way to achieve what I'm trying to achieve? – rookie Jul 07 '15 at 15:54
  • 2
    It conceptually makes no sense that way. – Francisco Goldenstein Jul 07 '15 at 15:56
  • 1
    make `A` internal, and keep `B` and `A` in the same assembly – Jonesopolis Jul 07 '15 at 15:56
  • Don't need an explicit internal declaration - this is the default behaviour - http://stackoverflow.com/questions/3763612/default-visibility-for-c-sharp-classes-and-members-fields-methods-etc http://stackoverflow.com/questions/2521459/what-are-the-default-access-modifiers-in-c - plus it's B that should be internal – Orphid Jul 07 '15 at 15:57
  • 2
    @Orphid, it maybe default, but by specifying it, you are making clear to others that you mean it, rather than just forgot to specify an access modifier. Same applies to `private` within a class. – David Arno Jul 07 '15 at 16:04
  • @DavidArno that's a matter of taste/ company style. Arguably someone familiar with C# will know the default access modifiers, and to add explicit modifiers can just seem like unnecessary verbiage - i.e. it's context dependent. It's still true to say that you "Don't need an explicit internal declaration". However, your point is taken. – Orphid Jul 07 '15 at 16:08
  • 2
    @Orphid, Sure it's subjective. It's not so much about knowing what the default access modifiers are though; it's all about making your intent clear. It improves maintainability. But it improves it in a subjective way. – David Arno Jul 07 '15 at 16:11
1

The default accesibility of classes is "internal", meaning they can only be accessed (legitimately) from within the declaring assembly. Further restrictions on visibility, as Francisco says, wouldn't make much sense for non-nested types.

Orphid
  • 2,722
  • 2
  • 27
  • 41