Access Modifiers in C# with Program Examples

Learn about access modifiers in C# such as public, private, protected and internal, with practical examples.

What are Access Modifiers?

Access modifiers are keywords that control the visibility and accessibility of classes, methods, and other members from other parts of a program.

public and private

The public modifier makes a member accessible from anywhere, while private restricts access to only within the same class, which is the most restrictive option.

protected and internal

The protected modifier allows access within the class and its derived classes, while internal restricts access to code within the same assembly.

Combining Modifiers

C# also allows combined modifiers like protected internal and private protected, giving finer control over how a member can be accessed across classes and assemblies.

Conclusion: Choosing the right access modifier for each class member is key to good encapsulation, helping you expose only what other code truly needs while keeping the rest safely hidden.

Ready to Master C# Programming Course?

Join Uncodemy's hands-on training program with expert mentors and real-world projects.