I have a class with some child (inner, not derived) classes. Ideally, I’d like some fields of the child classes to be modifiable by the parent, but not be public. Is there a design in C# that accomplishes this? I can think of no way to use the typical access modifiers (protected, private, public, etc)
For example, imagine
Class Car{
private Wheel wheel;
Class Wheel
{
//I want this to be only accessible within Car and Wheel
private int wheelSize;
}
}
So, That’s not my real code, but conveys the idea of what I want to do just fine.
You could derive a private inner class from
Wheelthat has additional public members. TheWheelclass would be abstract. this ensures that no instance of it can be created. Create a static method that creates wheels. The method would create aPrivateWheelbut return it asWheel. Wheels would always be exposed asWheelbut efectively be of typePrivateWheel.