Junior — Middle
Can you list the types of access modifiers and their features?
sobes.tech AI
Answer from AI
In C#, there are several access modifiers that define the visibility level of class members:
- public — accessible from anywhere in the program.
- private — accessible only within the same class or struct.
- protected — accessible within the class and its derived classes.
- internal — accessible within the same assembly.
- protected internal — accessible within the same assembly or from derived classes.
- private protected — accessible within the same assembly and from derived classes.
Example:
class Example
{
public int PublicValue; // Accessible everywhere
private int PrivateValue; // Only within Example
protected int ProtectedValue; // In Example and subclasses
internal int InternalValue; // Within the assembly
protected internal int ProtInt; // In subclasses or within the assembly
private protected int PrivProt; // In subclasses within the assembly
}