Encapsulation in C#

Last Updated : 12 Oct, 2026

Encapsulation in C# is an object-oriented programming concept that combines data and the methods that operate on that data into a single unit, such as a class. It also controls access to an object's internal state using access modifiers and provides controlled ways to read or modify the data.

  • Uses private fields to restrict direct access to an object's internal data.
  • Provides controlled access through methods, such as getters and setters, and properties, allowing validation before modifying data.
Encapsulation
Encapsulation

How Encapsulation Works

Encapsulation works by keeping an object's internal data within a class and exposing only the members needed to interact with it. In C#, encapsulation is commonly implemented using the following:

  • Private Fields: Store data that should not be directly accessible from outside the class.
  • Public Properties: Provide controlled access to read or modify private fields using get and set accessors.
  • Methods: Define operations that interact with or modify the object's internal state.
  • Access Modifiers: Determine which parts of the program can access specific class members.

For example, a bank account can keep its balance private and allow modifications only through methods such as Deposit() and Withdraw(). This prevents external code from changing the balance without following the account's rules.

Syntax of Encapsulation

A common way to implement encapsulation is by declaring a private field and providing a public property.

class ClassName{
private dataType fieldName;
public dataType PropertyName
{
get { return fieldName; }
set { fieldName = value; }
}
}

Here, fieldName stores the internal data, while PropertyName provides access through its get and set accessors.

  • get: Returns the current value of the field.
  • set: Assigns a new value to the field using the implicit value parameter.

Properties can also include validation or restrict modification by omitting or limiting access to a setter.

Example: Program to demonstrates encapsulation using a private field and a public property.

C#
using System;

class Programmer {
    private string name;

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

class Geeks {
    static void Main()
    {
        Programmer p = new Programmer();
        p.Name = "Geek";

        Console.WriteLine("Name => " + p.Name);
    }
}

Output
Name => Geek

Explanation: The name field is private, so it cannot be accessed directly from outside the Programmer class. The public Name property provides controlled access to read and modify the field.

Ways to Achieve Encapsulation

C# provides different mechanisms for controlling access to an object's data.

1. Using Access Modifiers

  • Access modifiers such as private, public, and protected control the accessibility of class members.
  • Declaring a field as private prevents unrelated external code from accessing it directly.

2. Using Properties

Properties provide a convenient way to expose data while controlling read and write operations.

For example:

public int Age { get; private set; }

Here, Age can be read by external code, but its setter is accessible only within the containing type.

3. Using Methods

Methods can control how an object's internal state changes by enforcing validation and business rules.

For example:

C#
public void Deposit(decimal amount){
    if (amount <= 0)
    {
        throw new ArgumentException(
            "Amount must be positive.");
    }

    balance += amount;
}

This method modifies the private balance field only when the deposit amount is valid.

Advantages of Encapsulation

  • Protects an object's internal data from direct access.
  • Allows validation before data is modified.
  • Makes code easier to maintain and update.
  • Keeps related data and methods together in one class.
  • Reduces dependency on the internal implementation of a class.
  • Provides better control over how an object's state is accessed or changed.

Limitations of Encapsulation

  • Can increase the amount of code when many properties or accessor methods are required.
  • May add unnecessary complexity to very simple classes.
  • Requires careful design to decide which data and operations should be accessible.
  • Excessive use of getters and setters can expose more of a class's internal state than necessary.

Data Hiding vs Encapsulation

Aspect

Data Hiding

Encapsulation

Meaning

Restricts direct access to internal data.

Combines data and related methods into a single unit.

Main focus

Controlling access to data.

Organizing data and behavior together.

Purpose

Protects internal state from unwanted direct access.

Provides a controlled and well-defined interface.

Commonly achieved using

Access modifiers such as private.

Classes, access modifiers, methods, and properties.

Scope

Focuses mainly on restricting access.

A broader concept that includes data hiding.

Example

private int balance;

A class containing a private balance field and a Deposit() method.

Comment

Explore