Access Modifiers in C#

Access modifiers in C# control the visibility and accessibility of classes, methods, properties, and other members. They define which parts of your code can access specific members, providing encapsulation and security to your applications.

Access Modifiers Overview

C# provides five access modifiers that determine the scope and accessibility of class members −

C# Access Modifiers Hierarchy public Most accessible protected internal internal protected private Least accessible

Public

The public modifier allows unrestricted access from anywhere in the application or other assemblies. Members declared as public have no access limitations.

public class MyClass {
    public int publicField;
    public void PublicMethod() { }
}

Private

The private modifier restricts access to within the same class only. Private members cannot be accessed from outside the declaring class, not even from derived classes.

public class MyClass {
    private int privateField;
    private void PrivateMethod() { }
}

Protected

The protected modifier allows access within the declaring class and its derived classes. Protected members are not accessible from unrelated classes.

Example

using System;

class Demo {
    protected string name = "Website";
    protected void Display(string str) {
        Console.WriteLine("Tabs: " + str);
    }
}

class Test : Demo {
    static void Main(string[] args) {
        Test t = new Test();
        Console.WriteLine("Details: " + t.name);
        t.Display("Product");
        t.Display("Services");
        t.Display("Tools");
        t.Display("Plugins");
    }
}

The output of the above code is −

Details: Website
Tabs: Product
Tabs: Services
Tabs: Tools
Tabs: Plugins

Internal

The internal modifier allows access within the same assembly (project). Internal members are accessible from any class within the same assembly but not from external assemblies.

Example

using System;

class InternalDemo {
    internal string projectName = "TutorialsPoint";
    internal void ShowInfo() {
        Console.WriteLine("Project: " + projectName);
    }
}

class Program {
    static void Main(string[] args) {
        InternalDemo demo = new InternalDemo();
        demo.ShowInfo();
        Console.WriteLine("Accessing internal member: " + demo.projectName);
    }
}

The output of the above code is −

Project: TutorialsPoint
Accessing internal member: TutorialsPoint

Protected Internal

The protected internal modifier combines both protected and internal access. Members are accessible within the same assembly OR from derived classes, even if they're in different assemblies.

Example

using System;

class BaseClass {
    protected internal string data = "Protected Internal Data";
    protected internal void ShowData() {
        Console.WriteLine("Data: " + data);
    }
}

class DerivedClass : BaseClass {
    public void AccessProtectedInternal() {
        Console.WriteLine("Accessing from derived class: " + data);
        ShowData();
    }
}

class Program {
    static void Main(string[] args) {
        BaseClass baseObj = new BaseClass();
        baseObj.ShowData();
        
        DerivedClass derivedObj = new DerivedClass();
        derivedObj.AccessProtectedInternal();
    }
}

The output of the above code is −

Data: Protected Internal Data
Accessing from derived class: Protected Internal Data
Data: Protected Internal Data

Access Modifiers Comparison

Access Modifier Same Class Derived Class Same Assembly Different Assembly
public ? ? ? ?
protected internal ? ? ? ? (derived only)
internal ? ? ? ?
protected ? ? ? ?
private ? ? ? ?

Conclusion

Access modifiers in C# provide essential control over code accessibility and encapsulation. Choose the most restrictive access level that still allows your code to function properly − use private by default, then increase accessibility as needed. This approach helps maintain code security and proper object-oriented design principles.

Updated on: 2026-03-17T00:02:47+05:30

800 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements