What Is Encapsulation in C#

Introduction

The object oriented programming will give the impression very unnatural to a programmer with a lot of procedural programming experience. In Object Oriented programming Encapsulation is the first pace. Encapsulation is the procedure of covering up of data and functions into a single unit (called class). An encapsulated object is often called an abstract data type. In this article let us see about it in a detailed manner.

Need for encapsulation

The need of encapsulation is to protect or prevent the code (data) from accidental corruption due to the silly little errors that we are all prone to make. In Object oriented programming data is treated as a critical element in the program development and data is packed closely to the functions that operate on it and protects it from accidental modification from outside functions.

Encapsulation provides a way to protect data from accidental corruption. Rather than defining the data in the form of public, we can declare those fields as private. The Private data are manipulated indirectly by two ways. Let us see some example programs in C# to demonstrate Encapsulation by those two methods. The first method is using a pair of conventional accessor and mutator methods. Another one method is using a named property. Whatever be the method our aim is to use the data with out any damage or change.

Encapsulation using accessors and mutators

Let us see an example of Department class. To manipulate the data in that class (String departname) we define an accessor (get method) and mutator (set method).

using System;
public class Department
{
    private string departname;
    public string Departname
    {
       
get
        {
            return departname;
        }
       
set
        {
            departname = value;
        }
    }
}
public class Departmentmain
{
    public static int Main(string[] args)
    {
        Department d = new Department();
        d.departname = "R A PATELL";
        Console.WriteLine("The Department is :{0}", d.Departname);
        return 0;
    }
}

Like the above way we can protect the private data from the outside world. Here we use two separate methods to assign and get the required data.

public static int Main(string[] args)
{
    Department d = new Department();
    d.SetDepartname("R A PATEL");
    Console.WriteLine("The Department is :"+d.GetDepartname());
    return 0;
}


In the above example we can't access the private data departname from an object instance. We manipulate the data only using those two methods.
Encapsulation using properties

Properties are a new language feature introduced with C#. Only a few languages support this property. Properties in C# helps in protect a field in a class by reading and writing to it. The first method itself is good but Encapsulation can be accomplished much smoother with properties.

Now let's see an example

using system;
public class Department
{
    private string departname;
    public string Departname
    {
       
get
        {
            return departname;
        }
       
set
        {
            departname = value;
        }
    }
}
public class Departmentmain
{
    public static int Main(string[] args)
    {
        Department d = new Department();
        d.departname = "R A PATELL";
        Console.WriteLine("The Department is :{0}", d.Departname);
        return 0;
    }
}

From the above example we see the usage of Encapsulation by using properties. The property has two accessor get and set. The get accessor returns the value of the some property field. The set accessor sets the value of the some property field with the contents of "value". Properties can be made read-only. This is accomplished by having only a get accessor in the property implementation.

Read only property

using system;
public class ReadDepartment
{
    private string departname;
    public ReadDepartment(string avalue)
    {
        departname = avalue;
    }
    public string Departname
    {
       
get
        {
            return departname;
        }
    }
}
public class ReadDepartmain
{
    public static int Main(string[] args)
    {
        ReadDepartment d = new ReadDepartment("R A PATELLL");
        Console.WriteLine("The Department is: {0}", d.Departname);
        return 0;
    }
}

In the above example we see how to implement a read-only property. The class ReadDepartment has a Departname property that only implements a get accessor. It leaves out the set accessor. This particular class has a constructor, which accepts a string parameter. The Main method of the ReadDepartmain class creates a new object named d. The instantiation of the d object uses the constructor of the ReadDepartment that takes a string parameter. Since the above program is read-only, we cannot set the value to the field departname and we only read or get the value of the data from the field. Properties can be made also Write-only. This is accomplished by having only a set accessor in the property implementation.

Write only property

using system;
public class WriteDepartment
{
    private string departname;
    public string Departname
    {
       
set
        {
            departname = value;
            Console.WriteLine("The Department is :{0}", departname);
        }
    }
}
public class WriteDepartmain
{
    public static int Main(string[] args)
    {
        WriteDepartment d = new WriteDepartment();
        d.departname = "R A PATELLL"; return 0;
    }
}

In the above example we see how to implement a Write-only property. The class WriteDepartment has now has a Departname property that only implements a set accessor. It leaves out the get accessor. The set accessor method is varied a little by it prints the value of the departname after it is assigned.

Conclusion

The Encapsulation is the first footstep towards the object-oriented programming. This article gives you a little bit information about Encapsulation. Using accessor and mutator methods we can make encapsulation. Another one method is using a named property. The benefit of properties is that the users of your objects are able to manipulate the internal data point using a single named item.

Next Recommended Reading Remove AM PM from Time String using C#