In this article I am going to discuss about C# Encapsulation with example in detail but before that if you had not read my previous article of C# access specifiers then must go through it. If you had read my previous articles then you must heard about encapsulation which is one of the pillars of object-oriented programming.

Introduction to C# Encapsulation

In any programming language including C# Encapsulation is one of the important fundamentals of object-oriented programming principle which is used to wrapping up of data members and member functions into a single unit. This principle ensures that the state, behaviour of this single unit cannot be directly accessible from other units. C# access specifiers play crucial role to achieve encapsulation. In C# every class, interface, struct, enums that we create is an example of encapsulation because we can wrap our all data members and member functions inside them and then use as a single unit to access this.

C# Data-hiding

C# Data-hiding is the process of hiding the internal data from outside world and show only the essential data to users. Without encapsulation we cannot achieve data-hiding. The main purpose of data-hiding is to secure our confidential data from outsiders so they cannot use this data for any malpractice or for any wrong reasons. For example, if you have a class named Bike and you want to keep the bike’s speed private, then you can declare the BikeSpeed variable as private, like given below:

class Bike {
    private int BikeSpeed;   //This will declare private variable so not accessible through outside class
}

Advantages of Encapsulation

1. Data-Hiding : Encapsulation allows the internal state of an object to be hidden from the outside world. This is achieved by using access modifiers. By restricting access to the object’s data, encapsulation helps to prevent unauthorized or accidental modification of data.

2. Maintainability : Since the internal implementation details of an object are hidden, it is easier to change and maintain the code. Changes to the internal implementation do not affect the external code that uses the object, as long as the public interface remains the same.

3. Modularity : Encapsulation promotes the design of modular classes. Each class encapsulates its data and behavior, making it easier to develop, test, and debug individual components.

4. Flexibility : Encapsulation provides the flexibility to change the internal workings of a class without affecting its external behavior. This allows developers to improve and optimize the code without worrying about breaking the code that depends on it.

5. Security : By restricting direct access to the data, encapsulation adds a layer of security. Sensitive data can be protected from being exposed or manipulated directly, reducing the risk of unintended or malicious changes.

Implement Encapsulation in C#

In C#, Encapsulation can be implemented using two ways. The first way is to use the private access specifier to members so they cannot be accessible from any other class. The second way is to use the pair of getter and setter to access the private member of class.

C# Program to Understand Encapsulation with using Getter and Setter

using System;
namespace CsharpEncapsulationDemo
{
    public class Bank
    {
        //Hiding class data by declaring the variable as private
        private double balance;

        //Creating public Setter and Getter methods

        //Public Getter Method
        //This method is used to return the data stored in the balance variable
        public double GetBalance()
        {
            return balance;
        }

        //Public Setter Method
        //This method is used to stored the data  in the balance variable
        public void SetBalance(double balance)
        {
            this.balance = balance;
        }
    }
    class Program
    {
        public static void Main()
        {
            Bank bank = new Bank();
            //You cannot access the Private Variable
            //bank.balance; //Compile Time Error

            //But you can access private variable via public setter and getter methods
            bank.SetBalance(500);
            Console.WriteLine(bank.GetBalance());
            Console.ReadKey();
        }
    }
}
Output:
500

In above program you can see that I have declared one variable with private access specifier and then accessing that variable with public getter and setter functions. If you try to access the variable directly then you get compile time error.

Conclusion

In this C# tutorials article I have discussed about Encapsulation with example. Hope you understand this and like this article. But try to write at least one program to implement encapsulation so you can do it at your end. In my next article I am going to discuss about inheritance in detail with proper examples.