Junior — Middle
What are the access levels for class elements in object-oriented programming?
sobes.tech AI
Answer from AI
In object-oriented programming in C#, the following are the main access levels for class members:
- public — access is open to all, members are accessible from any code.
- private — access is restricted to within the class itself.
- protected — access is allowed within the class and its descendants.
- internal — access is allowed within the current assembly.
- protected internal — access is allowed either from the current assembly or from derived classes.
- private protected — access is allowed from derived classes, but only within the current assembly.
Example:
class Example
{
public int PublicField; // Accessible everywhere
private int PrivateField; // Only within Example
protected int ProtectedField; // In Example and descendants
internal int InternalField; // Within the assembly
}