Published on: June 7, 2025
Category: C# Basics
Tags: CSharp, Access Modifiers, Encapsulation, OOP, Interview Questions, public private protected internal, .NET Dev Corner, Class Design, .NET Fundamentals
✨ Introduction
Access modifiers are essential in C# for controlling how members of a class (variables, methods, constructors, etc.) are exposed to other parts of your code. They are the cornerstone of encapsulation, one of the pillars of object-oriented programming.
🔓 1. public
- Access Level: Accessible from anywhere in the project or external assemblies.
- Use When: You want complete access to a method or class member.
public class Car
{
public string Brand;
public void Start() => Console.WriteLine("Engine started");
}
🧠 Think of public
as a highway — anyone can drive through.
🔒 2. private
- Access Level: Accessible only within the containing class.
- Use When: You want to encapsulate internal logic or variables.
public class BankAccount
{
private decimal balance = 0;
public void Deposit(decimal amount) => balance += amount;
}
🧠 private
protects your internal data from accidental misuse.
🛡️ 3. protected
- Access Level: Accessible within the containing class and its subclasses (even in other assemblies).
- Use When: You’re building a base class meant to be extended.
public class Animal
{
protected void Breathe() => Console.WriteLine("Breathing...");
}
public class Dog : Animal
{
public void Act() => Breathe();
}
🧠 protected
is like family access — inherited members get to use it.
🏠 4. internal
- Access Level: Accessible only within the same assembly (project).
- Use When: You want to hide members from other assemblies but expose them within yours.
internal class FileHelper
{
internal void CleanTempFiles() { }
}
🧠 Think of internal
as company access — shared within the same project only.
🧬 5. protected internal
- Access Level: Accessible within the same assembly or from derived classes in other assemblies.
public class Logger
{
protected internal void Log(string message) => Console.WriteLine(message);
}
🧬 6. private protected
(C# 7.2+)
- Access Level: Accessible within the same assembly and only in derived classes.
public class Settings
{
private protected void Load() => Console.WriteLine("Loading settings");
}
🔍 Comparison Table
Modifier | Same Class | Derived Class | Same Assembly | External Assembly |
---|---|---|---|---|
public | ✅ | ✅ | ✅ | ✅ |
private | ✅ | ❌ | ❌ | ❌ |
protected | ✅ | ✅ | ❌ | ✅ (if inherited) |
internal | ✅ | ✅ | ✅ | ❌ |
protected internal | ✅ | ✅ | ✅ | ✅ (if inherited) |
private protected | ✅ | ✅ | ✅ | ❌ |
🎯 When to Use Which Modifier?
Expose to all projects | public |
Hide implementation details | private |
Share with child classes only | protected |
Share within the same assembly | internal |
Advanced hybrid control | protected internal , private protected |
💡 Interview Tip
Expect to be asked:
- “What’s the difference between internal and protected internal?”
- “How do you apply encapsulation in C#?”
📌 Final Thoughts
Access modifiers define how your code communicates internally and externally. Choose wisely for cleaner, scalable design.
🔐 Encapsulation starts with proper access control.
📬 Stay Connected
- 🔗 Blog: www.ajaygangwar.com
- 💼 LinkedIn: Ajay Gangwar
- 📧 Email: seajaygangwar@gmail.com