Reputation: 2923
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.
Upvotes: 1
Views: 105
Reputation: 2852
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.
Upvotes: 1
Reputation: 13777
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.
Upvotes: 6