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

21 / 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/Encapsulation in C#
🔷C# Programming

Encapsulation in C#

Updated 2026-05-15
10 min read

Encapsulation in C#

Introduction

Encapsulation is a fundamental concept in object-oriented programming (OOP) that involves bundling the data (attributes) and methods (functions) that operate on the data into a single unit or class. It also restricts direct access to some of an object's components, which can prevent the accidental modification of data. This tutorial will cover encapsulation in C#, focusing on how to use access modifiers to control the visibility and accessibility of class members.

Concept

Encapsulation helps in protecting the integrity of the data by restricting its exposure to unauthorized access. In C#, this is achieved through the use of access modifiers, which define the scope and visibility of class members such as fields, properties, methods, and constructors.

Access Modifiers in C#

C# provides several access modifiers that control the accessibility of class members:

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

Examples

Let's explore how these access modifiers work through practical examples.

Example 1: Basic Encapsulation

public class Person
{
    private string name;
    private int age;

    public Person(string name, int age)
    {
        this.name = name;
        this.age = age;
    }

    public void DisplayInfo()
    {
        Console.WriteLine($"Name: {name}, Age: {age}");
    }
}

class Program
{
    static void Main()
    {
        Person person = new Person("John Doe", 30);
        person.DisplayInfo();
    }
}

In this example, the `Person` class has two private fields, `name` and `age`, which are not accessible from outside the class. The constructor initializes these fields, and a public method `DisplayInfo` is provided to display their values.

### Example 2: Using Properties for Encapsulation

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

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public int Age
    {
        get { return age; }
        set 
        { 
            if (value >= 0)
                age = value;
            else
                throw new ArgumentException("Age cannot be negative.");
        }
    }

    public Person(string name, int age)
    {
        this.name = name;
        this.age = age;
    }

    public void DisplayInfo()
    {
        Console.WriteLine($"Name: {name}, Age: {age}");
    }
}

class Program
{
    static void Main()
    {
        Person person = new Person("John Doe", 30);
        person.DisplayInfo();

        // Accessing properties
        person.Name = "Jane Doe";
        person.Age = 25;
        person.DisplayInfo();
    }
}

In this example, the `Person` class uses properties (`Name` and `Age`) to encapsulate the private fields. Properties provide a way to get and set the values of the fields while allowing additional logic (like validation) to be executed.

### Example 3: Access Modifiers in Action

```csharp
public class Employee
{
    public string Name { get; set; }
    protected int Salary { get; set; }

    public Employee(string name, int salary)
    {
        Name = name;
        Salary = salary;
    }

    public void DisplayInfo()
    {
        Console.WriteLine($"Name: {Name}, Salary: {Salary}");
    }
}

class Manager : Employee
{
    public Manager(string name, int salary) : base(name, salary) {}

    public void ShowSalary()
    {
        // Accessing protected member from derived class
        Console.WriteLine($"Manager's Salary: {Salary}");
    }
}

class Program
{
    static void Main()
    {
        Employee emp = new Employee("Alice", 5000);
        emp.DisplayInfo();
        // emp.Salary; // This will cause a compile-time error as Salary is protected

        Manager mgr = new Manager("Bob", 7000);
        mgr.ShowSalary();
    }
}

In this example, the `Employee` class has a public property `Name` and a protected property `Salary`. The `Manager` class, which inherits from `Employee`, can access the `Salary` property because it is protected. However, trying to access `Salary` directly from an instance of `Employee` outside the class hierarchy will result in a compile-time error.

## What's Next?

In this tutorial, we covered the basics of encapsulation in C# using access modifiers and properties. In the next section, we will delve deeper into "Access Modifiers in C#" to explore more advanced scenarios and best practices for controlling member visibility.

PreviousPolymorphism in C#Next Access Modifiers in C#

Recommended Gear

Polymorphism in C#Access Modifiers in C#