Advertisement
Beginner C# Programming, Access Modifiers in C#

What are Access Modifiers in C#?

Short answer

Access modifiers in C# are keywords that control the visibility and accessibility of classes, methods, properties, and other members from other parts of the code.

Save C# Programming mock interview
Advertisement
AModel answer
Access modifiers in C# are keywords that control the visibility and accessibility of classes, methods, properties, and other members from other parts of the code. The main access modifiers are public, which allows access from anywhere, private, which restricts access to only within the same class, protected, which allows access within the same class and any derived classes, internal, which allows access only within the same assembly, and protected internal, which combines protected and internal accessibility rules.
public class Customer {
    public string Name;       // accessible from anywhere
    private decimal _balance; // accessible only within Customer
    protected int LoyaltyPoints; // accessible in Customer and its subclasses
}
Real-world example
A banking application marks its internal balance calculation logic as private, ensuring that no other class anywhere in the codebase can directly modify the balance except through the class's own carefully validated methods.
Likely follow-ups
  • Which access modifier is used by default if none is specified?
  • How do access modifiers support the principle of encapsulation?
Related topics