codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

C# Programming

22 / 60 topics
17Classes and Objects18Constructors in C#19Inheritance in C#20Polymorphism in C#21Encapsulation in C#22Access Modifiers in C#23Properties in C#24Indexers in C#25Events in C#26Delegates in C#
Tutorials/C# Programming/Access Modifiers in C#
🔷C# Programming

Access Modifiers in C#

Updated 2026-05-15
10 min read

Access Modifiers in C#

Introduction

In object-oriented programming, encapsulation is a fundamental concept that involves bundling the data (attributes) and methods that operate on the data into a single unit or class. Encapsulation also restricts direct access to some of an object's components, which can prevent the accidental modification of data. Access modifiers in C# are keywords used to set the accessibility level of classes, methods, properties, fields, and other members.

Concept

Access modifiers determine how accessible a class member is from outside its own class. C# provides several access modifiers that control this visibility:

  1. public: The member can be accessed from any code in the same assembly or another assembly that references it.
  2. private: The member can only be accessed within its own class.
  3. protected: The member can be accessed within its own class, derived classes, and classes in the same assembly.
  4. internal: The member can be accessed from any code in the same assembly, but not from another assembly.
  5. protected internal: The member can be accessed from any code in the same assembly or from derived classes in other assemblies.

Examples

Let's explore each access modifier with practical examples.

Public Access Modifier

public class Person
{
    public string Name { get; set; }

    public void Display()
    {
        Console.WriteLine("Name: " + Name);
    }
}

class Program
{
    static void Main(string[] args)
    {
        Person person = new Person();
        person.Name = "John Doe";
        person.Display();
    }
}

<OutputBlock>
{`Name: John Doe`}

In this example, the `Name` property and the `Display` method are public. Therefore, they can be accessed from outside the `Person` class.

### Private Access Modifier

```csharp
public class Person
{
    private string name;

    public void SetName(string newName)
    {
        name = newName;
    }

    public void Display()
    {
        Console.WriteLine("Name: " + name);
    }
}

class Program
{
    static void Main(string[] args)
    {
        Person person = new Person();
        // person.name = "John Doe"; // This will cause a compile-time error
        person.SetName("John Doe");
        person.Display();
    }
}

{`Name: John Doe`}
</OutputBlock>

Here, the `name` field is private. It cannot be accessed directly from outside the `Person` class. Instead, we use a public method `SetName` to modify it.

### Protected Access Modifier

```csharp
public class Person
{
    protected string name;

    public void SetName(string newName)
    {
        name = newName;
    }

    public void Display()
    {
        Console.WriteLine("Name: " + name);
    }
}

public class Employee : Person
{
    public void ShowName()
    {
        Console.WriteLine("Employee Name: " + name); // Accessible because it's a derived class
    }
}

class Program
{
    static void Main(string[] args)
    {
        Employee employee = new Employee();
        employee.SetName("Jane Doe");
        employee.Display();
        employee.ShowName();
    }
}

<OutputBlock>
{`Name: Jane Doe
Employee Name: Jane Doe`}
</OutputBlock>

In this example, the `name` field is protected. It can be accessed within the `Person` class and any derived classes like `Employee`.

### Internal Access Modifier

```csharp
internal class Person
{
    public string Name { get; set; }

    public void Display()
    {
        Console.WriteLine("Name: " + Name);
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Person person = new Person(); // This will cause a compile-time error if the classes are in different assemblies
        // person.Name = "John Doe";
        // person.Display();
    }
}

In this example, the Person class is internal. It can only be accessed within the same assembly.

Protected Internal Access Modifier

protected internal class Person
{
    public string Name { get; set; }

    public void Display()
    {
        Console.WriteLine("Name: " + Name);
    }
}

public class Employee : Person
{
    public void ShowName()
    {
        Console.WriteLine("Employee Name: " + Name); // Accessible because it's a derived class in another assembly
    }
}

class Program
{
    static void Main(string[] args)
    {
        Employee employee = new Employee();
        employee.Name = "Jane Doe";
        employee.Display();
        employee.ShowName();
    }
}
Output
Name: Jane Doe
Employee Name: Jane Doe

Here, the Person class is protected internal. It can be accessed within the same assembly or from derived classes in other assemblies.

What's Next?

After mastering access modifiers, you should explore how to use them effectively with properties in C#. Properties provide a way to encapsulate fields and control their access through get and set methods. Understanding properties will further enhance your ability to implement robust and secure object-oriented designs.

Stay tuned for more tutorials on advanced C# concepts!


PreviousEncapsulation in C#Next Properties in C#

Recommended Gear

Encapsulation in C#Properties in C#