Bhubaneswar, Odisha
+91-8328865778

Encapsulation In C#

Encapsulation In C#

The process of binding the data and functions together into a single unit (i.e. class) is called encapsulation or you can say that the process of defining a class by hiding its internal data members from outside the class and accessing those internal data members only through publicly exposed methods (setter and getter methods) or properties with proper validations is called encapsulation.

Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates. In a different way, encapsulation is a protective shield that prevents the data from being accessed by the code outside this shield.

Encapsulation
  • Technically in encapsulation, the variables or data of a class are hidden from any other class and can be accessed only through any member function of own class in which they are declared.
  • As in encapsulation, the data in a class is hidden from other classes, so it is also known as data-hiding.
  • Encapsulation in C# can be used by: Declaring all the variables in the class as private and using C# Properties in the class to set and get the values of variables.

C# Encapsulation Example

Following is the example of defining an encapsulated class in c# programming language.

 class User
    {
       private string location;
       private string name;
       public string Location
       {
          get
          {
             return location;
          }
          set
          {
             location = value;
          }
       }
       public string Name
       {
         get
         {
             return name;
         }
         set
         {
            name = value;
         }
       }
    }
    class Program
    {
       static void Main(string[] args)
       {
          User u = new User();
          // set accessor will invoke
          u.Name = "Suresh Dasari";
          // set accessor will invoke
          u.Location = "Hyderabad";
          // get accessor will invoke
          Console.WriteLine("Name: " + u.Name);
          // get accessor will invoke
          Console.WriteLine("Location: " + u.Location);
          Console.WriteLine("\nPress Enter Key to Exit..");
          Console.ReadLine();
       }
    }
}

As shown in above example, we defined fields in encapsulated class using properties, and we are able to manipulate field values using get and set accessors of properties.

Benifits of Encapsulation:

  • Data Hiding: The user will have no idea about the inner implementation of the class. It will not be visible to the user that how the class is stored values in the variables. He only knows that we are passing the values to accessors and variables are getting initialized to that value.
  • Increased Flexibility: We can make the variables of the class as read-only or write-only depending on our requirement. If we wish to make the variables as read-only then we have to only use Get Accessor in the code. If we wish to make the variables as write-only then we have to only use Set Accessor.
  • Reusability: Encapsulation also improves the re-usability and easy to change with new requirements.
  • Testing code is easy: Encapsulated code is easy to test for unit testing.