8️⃣ C# Object-Oriented Programming (OOP)
Estimated reading: 4 minutes 96 views

πŸ—οΈ C# Access Modifiers

🧲 Introduction – Why Access Modifiers Matter in C#?

Access modifiers are fundamental in controlling how members of a class are exposed to other parts of a program. They are crucial to achieving encapsulation, one of the pillars of object-oriented programming in C#. Proper use of access modifiers helps protect internal data, enforce APIs, and improve maintainability.

🎯 In this guide, you’ll learn:

  • What access modifiers are and how they work
  • The different types of access levels in C#
  • How to apply them to classes, fields, properties, methods, and constructors
  • Best practices for visibility and security in class design

πŸ” Core Concept – What Are Access Modifiers in C#?

Access modifiers define the visibility scope of types and type members (fields, properties, methods, etc.).

C# Provides the Following Access Modifiers:

ModifierAccess Level
publicAccessible from anywhere
privateAccessible only within the containing class
protectedAccessible within the class and derived classes
internalAccessible within the same assembly
protected internalAccessible within same assembly or derived class
private protectedAccessible within the same assembly and derived class

πŸ’» Code Examples – With Explanations

🧱 Example 1: Public and Private

public class User
{
    private string password;
    public string Username;

    public void SetPassword(string pwd)
    {
        password = pwd;
    }
}

πŸ”Ή Explanation:

  • password is private – cannot be accessed outside User
  • Username is public – accessible anywhere

πŸ›‘οΈ Example 2: Protected and Internal

public class BaseClass
{
    protected int protectedValue = 42;
    internal int internalValue = 99;
}

public class DerivedClass : BaseClass
{
    public void ShowValues()
    {
        Console.WriteLine(protectedValue); // OK
        Console.WriteLine(internalValue);  // OK
    }
}

πŸ”Ή Explanation:

  • protectedValue accessible in the derived class
  • internalValue accessible within the same assembly

πŸŒ€ Example 3: Protected Internal vs Private Protected

public class Base
{
    protected internal int x = 10;
    private protected int y = 20;
}
  • protected internal: accessible from any derived class or any code in the same assembly
  • private protected: accessible from a derived class within the same assembly only

πŸ’‘ Best Practices & Tips

πŸ“˜ Best Practices

  • Keep class fields private and expose them via properties
  • Use public only when absolutely necessary
  • Limit visibility to what’s needed, no more

πŸ’‘ Tips

  • Combine protected and internal for flexible access in class libraries
  • Use private protected for secure internal inheritance

⚠️ Pitfalls

  • Overusing public exposes implementation and breaks encapsulation
  • Misusing internal can unintentionally expose data across unrelated classes

πŸ“Š Access Modifier Comparison Table

ModifierSame ClassDerived Class (Same Assembly)Other Classes (Same Assembly)Derived Class (Other Assembly)Other Classes (Other Assembly)
publicβœ…βœ…βœ…βœ…βœ…
privateβœ…βŒβŒβŒβŒ
protectedβœ…βœ…βŒβœ…βŒ
internalβœ…βœ…βœ…βŒβŒ
protected internalβœ…βœ…βœ…βœ…βŒ
private protectedβœ…βœ…βŒβŒβŒ

πŸ› οΈ Use Cases – Where Access Modifiers Are Critical

  • Library Development: Expose only public APIs, keep implementation details private
  • Secure Code: Prevent unauthorized access to sensitive data (private or protected)
  • Inheritance: Safely share behavior with protected and private protected
  • Assembly-Level Design: Use internal to restrict visibility to components

πŸ“Œ Summary – Recap & Next Steps

Access modifiers in C# are essential tools for data encapsulation, API design, and secure coding. By limiting visibility, you improve maintainability, avoid misuse, and enforce clean coding principles.

πŸ” Key Takeaways:

  • Use private by default, expose only what’s needed
  • protected enables controlled inheritance
  • internal and protected internal help in assembly-wide design
  • private protected is ideal for secure framework design

βš™οΈ Real-World Relevance:
Access modifiers control how parts of your app interactβ€”be it in ASP.NET APIs, class libraries, or game components in Unity.


❓ FAQ – C# Access Modifiers

❓ What is the default access modifier for class members?
βœ… private for members inside a class.

❓ Can I use access modifiers with class declarations?
βœ… Yes, but top-level classes can only be public or internal.

❓ What’s the difference between protected and private?
βœ… protected allows access in derived classes, private restricts it to the same class.

❓ Can a method be protected internal?
βœ… Yes, it’s accessible within the same assembly or from derived classes in other assemblies.

❓ Should I make fields public?
βœ… ❌ No. Always encapsulate fields using private + public/protected properties.


Share Now :
Share

πŸ—οΈ C# Access Modifiers

Or Copy Link

CONTENTS
Scroll to Top