Access modifiers in C#
public - visible everywhere
public class Order
{
public int Id { get; set; } // accessible from ANYWHERE - another class, another assembly
}
No visibility restrictions whatsoever. Used for the part of a class's API that is meant to be called from outside - e.g. methods of services injected via DI, properties of DTOs.
private - visible only inside the same class
public class OrderService
{
private readonly AppDbContext _context; // visible ONLY inside OrderService
private bool IsValidOrder(Order order) => order.Items.Count > 0; // only this class can call it
}
The default modifier if you write nothing (for class fields and methods - a class itself is internal by default). The standard for fields holding dependencies (_context, _logger) and helper methods not needed outside - encapsulation in its purest form.
protected - visible in this class and derived classes
public abstract class ViewModelBase
{
protected void SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
// ...
}
}
public class OrderViewModel : ViewModelBase
{
public void UpdateStatus(string status)
{
SetProperty(ref _status, status); // ✅ accessible, because OrderViewModel INHERITS from ViewModelBase
}
}
var vm = new OrderViewModel();
vm.SetProperty(...); // ❌ Error - inaccessible from OUTSIDE, even if you have an instance
A classic use in the Template Method Pattern - helper methods meant to be used by subclasses, but not by "foreign" code.
internal - visible in the same assembly (project)
// W projekcie MyApp.Core
internal class DatabaseMigrationHelper
{
internal void RunMigration() { }
}
// W tym samym assembly (MyApp.Core) - OK
var helper = new DatabaseMigrationHelper();
// In ANOTHER assembly (e.g. MyApp.Web, referencing MyApp.Core) - compile error
var helper = new DatabaseMigrationHelper(); // ❌ niewidoczne
Useful for classes/types of a library's internal implementation that shouldn't be part of the public API - a consumer of your library (.dll) shouldn't have access to them, but different classes inside the same project can see each other.
protected internal - the union of permissions (logical OR)
public class Base
{
protected internal void Method() { }
}
Visible if either condition is met: a derived class (even in a different assembly) OR any class in the same assembly (even without inheritance).
// Ten sam assembly, BEZ dziedziczenia po Base
public class Unrelated
{
void Test(Base b) => b.Method(); // ✅ OK - ten sam assembly wystarcza
}
// Inny assembly, Z dziedziczeniem po Base
public class DerivedInOtherAssembly : Base
{
void Test() => Method(); // ✅ OK - dziedziczenie wystarcza, mimo innego assembly
}
// Inny assembly, BEZ dziedziczenia
public class UnrelatedInOtherAssembly
{
void Test(Base b) => b.Method(); // ❌ Error - neither the same assembly nor inheritance
}
private protected - the intersection of permissions (logical AND) - C# 7.2+
public class Base
{
private protected void Method() { }
}
Visible only if both conditions are met at the same time: a derived class AND in the same assembly.
// The same assembly, inheritance - the ONLY case that works
public class Derived : Base
{
void Test() => Method(); // ✅ OK
}
// Inny assembly, dziedziczenie - NIE wystarczy
public class DerivedInOtherAssembly : Base
{
void Test() => Method(); // ❌ Error - a different assembly, despite inheritance
}
This is the most restrictive form of "protected" - useful when you want to expose something to subclasses, but only within your own library, not to external consumers who could inherit from your public base class.
Summary table
| Modifier | The same class | Derived class (same assembly) | Derived class (different assembly) | Any class (same assembly) | Any class (different assembly) |
|---|---|---|---|---|---|
public |
✅ | ✅ | ✅ | ✅ | ✅ |
protected internal |
✅ | ✅ | ✅ | ✅ | ❌ |
internal |
✅ | ✅ | ❌ | ✅ | ❌ |
protected |
✅ | ✅ | ✅ | ❌ | ❌ |
private protected |
✅ | ✅ | ❌ | ❌ | ❌ |
private |
✅ | ❌ | ❌ | ❌ | ❌ |