C# Interview Question #25

What are access modifiers in C#?

Classes, Constructors & Properties Junior Beginner

Quick Interview Answer

Access modifiers determine where a type or member can be accessed.

Detailed Explanation

Access modifiers determine where a type or member can be accessed.

public means accessible wherever the containing type is accessible. private restricts access to the containing type. protected allows access from the containing type and derived classes. internal restricts access to the same assembly. protected internal and private protected combine assembly and inheritance rules.

Using the narrowest appropriate access level improves encapsulation and reduces accidental coupling.

Code Example

public string Name { get; set; }
private decimal _salary;
protected void CalculateSalary() { }
internal class Helper { }