Open In App

Encapsulation in C#

Last Updated : 17 Sep, 2025
Comments
Improve
Suggest changes
82 Likes
Like
Report

Encapsulation is one of the core principles of object-oriented programming (OOP). It refers to the practice of binding data (fields) and the methods that operate on that data into a single unit, while restricting direct access to some components. This ensures controlled interaction with an object’s internal state.

ecapsulation
Encapsulation

Key Concepts

  • Encapsulation hides the internal representation of an object and exposes only necessary operations.
  • Fields are often kept private while access is provided through public properties or methods.
  • It improves data security, code maintainability and flexibility.
  • Access modifiers (private, public, protected, internal) control visibility of members.

Example:

C#
using System;

class Account
{
    private double balance; // hidden field

    public void Deposit(double amount)
    {
        if (amount > 0)
            balance += amount;
    }

    public void Withdraw(double amount)
    {
        if (amount <= balance)
            balance -= amount;
    }

    public double GetBalance()
    {
        return balance;
    }
}

class GFG
{
    static void Main()
    {
        Account acc = new Account();
        acc.Deposit(500);
        acc.Withdraw(200);
        Console.WriteLine("Balance: " + acc.GetBalance());
    }
}

Output
Balance: 300

Explanation:

  • balance is private and cannot be accessed directly from outside.
  • The class provides controlled methods (Deposit, Withdraw, GetBalance) to interact with the field.

Encapsulation Using Properties

C# provides properties to simplify encapsulation, acting like smart getters and setters.

C#
using System;

class Student
{
    private string name; // private field

    public string Name
    {
        get { return name; }
        set 
        { 
            if (!string.IsNullOrEmpty(value))
                name = value; 
        }
    }
}
class Program
{
    static void Main()
    {
        Student s = new Student();
        s.Name = "Alex";
        Console.WriteLine("Student Name: " + s.Name);
    }
}

Output
Student Name: Alex

Explanation:

  • The field name is private.
  • The Name property controls how values are set and retrieved.
  • This improves flexibility compared to public fields.

Advantages

  • Data Protection: Prevents unauthorized access to fields.
  • Controlled Access: Exposes only required operations.
  • Code Flexibility: Internal implementation can change without affecting external code.
  • Maintainability: Reduces coupling between classes.

Disadvantages

  • Using getters and setters adds extra code compared to accessing fields directly.
  • Accessing data through methods may be a bit slower than direct access.
  • Since data is hidden, it can sometimes be difficult to quickly inspect or change values during debugging.
Suggested Quiz
4 Questions

Which of the following best defines encapsulation in C#?

  • A

    Combining multiple classes into one

  • B

    Wrapping data and methods into a single unit with controlled access

  • C

    Hiding class implementation by using inheritance

  • D

    Using multiple access modifiers in the same class

Explanation:

Encapsulation means binding data (fields) and methods (functions) into a single unit (class) and restricting direct access by using private fields with public properties or methods.

In the BankAccount example, why can’t we directly modify the balance field?

  • A

    Because it is declared as protected

  • B

    Because it is declared as private

  • C

    Because it is declared as readonly

  • D

    Because it is declared as static

Explanation:

The balance field is private, so it cannot be accessed directly from outside the class. Instead, methods like Deposit, Withdraw, and GetBalance are used.

Which of the following best describes Encapsulation in C#?

  • A

    Inheriting properties from one class into another

  • B

    Binding data and methods together while restricting direct access to data

  • C

    Allowing one method to exist in multiple forms


  • D

    Separating code into multiple files

Explanation:


In the Account class example, why is the balance variable declared as private?

  • A

    To make it accessible from any class

  • B

    To hide it from the class itself

  • C

    To restrict direct access and allow controlled modification through methods

  • D

    To improve program execution speed

Explanation:


Quiz Completed Successfully
Your Score :   2/4
Accuracy :  0%
Login to View Explanation
1/4 1/4 < Previous Next >

Article Tags :

Explore