What are Access Modifiers in C#?

πŸ’‘ Concept: Access Modifiers

Access modifiers in C# determine the visibility and accessibility of classes, methods, and members. They help enforce encapsulation and limit unwanted exposure of code components.

πŸ“˜ Quick Intro

C# offers several access levels: public, private, protected, internal, and protected internal. Each defines how and where members can be accessed within your application or assemblies.

🧠 Analogy

Think of access modifiers like doors in a building. Public doors are open to everyone, private doors are locked for personal use, protected ones allow family, and internal doors are accessible only within your organization.

πŸ”§ Technical Explanation

  • πŸ”“ public: Accessible from anywhere.
  • πŸ” private: Accessible only within the same class.
  • πŸ”’ protected: Accessible within the class and its subclasses.
  • 🏒 internal: Accessible only within the same assembly.
  • πŸ”πŸ’ protected internal: Accessible from derived classes or within the same assembly.
  • πŸ”πŸ›‘οΈ private protected: Accessible only within the declaring class or derived classes within the same assembly (C# 7.2+).

🎯 Use Cases

  • βœ… Use public for APIs or classes consumed externally.
  • βœ… Use private to protect helper methods or sensitive data.
  • βœ… Use protected for base class extensibility.
  • βœ… Use internal for components used only within the current project.
  • βœ… Use private protected for the tightest inheritance-based control.

πŸ’» Code Example

public class MyClass
{
    public int PublicValue;
    private int PrivateValue;
    protected int ProtectedValue;
    internal int InternalValue;
    protected internal int ProtectedInternalValue;
    private protected int PrivateProtectedValue;

    public void DisplayValues()
    {
        Console.WriteLine(PublicValue);
        Console.WriteLine(PrivateValue);
        Console.WriteLine(ProtectedValue);
        Console.WriteLine(InternalValue);
        Console.WriteLine(ProtectedInternalValue);
        Console.WriteLine(PrivateProtectedValue);
    }
}

❓ Interview Q&A

Q1: What is the purpose of access modifiers in C#?
A: To control visibility and enforce encapsulation.

Q2: Which is the most restrictive modifier?
A: private.

Q3: What does protected internal mean?
A: Accessible from the same assembly or any derived class.

Q4: Can you access a private member in another class?
A: No, unless using reflection.

Q5: What is internal mainly used for?
A: To expose types/methods only within the same project/assembly.

Q6: Is protected the same as internal?
A: No, protected is for inheritance; internal is assembly-wide.

Q7: Can structs have protected members?
A: No, structs do not support inheritance.

Q8: What version introduced private protected?
A: C# 7.2.

Q9: How do access modifiers support OOP principles?
A: They enable encapsulation and abstraction.

Q10: Can access modifiers be applied to local variables?
A: No, only class or member level elements.

πŸ“ MCQs

Q1. Which modifier allows access from any code?

  • private
  • protected
  • internal
  • public

Q2. Which modifier restricts access to same class only?

  • public
  • internal
  • private
  • protected

Q3. Which one allows access in same assembly or subclass?

  • internal
  • protected
  • public
  • protected internal

Q4. Which modifier is new in C# 7.2?

  • protected
  • private
  • protected internal
  • private protected

Q5. Can a struct have protected members?

  • Yes
  • No
  • Only in records
  • Only in classes

Q6. Which modifier is used for internal project sharing?

  • protected
  • internal
  • private
  • global

Q7. What does encapsulation mean?

  • Inheritance
  • Polymorphism
  • Hiding internal state
  • Abstraction only

Q8. Which is NOT a valid access modifier?

  • internal
  • external
  • private
  • protected

Q9. Which access level is used in APIs?

  • internal
  • private
  • public
  • protected internal

Q10. Can local variables be marked public?

  • Yes
  • Only in functions
  • No
  • Only in classes

πŸ’‘ Bonus Insight

Access modifiers help you design clean APIs and control encapsulation. Understanding them improves maintainability and protects code against misuse or unintended access.

πŸ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

πŸ” Navigation

πŸ’¬ Feedback
πŸš€ Start Learning
Share:

Tags: