C# Encapsulation

What is Encapsulation

Encapsulation is the process of hiding data or encapsulating it from the outside world.

Why need Encapsulation

➤ To make a class more secure and reliable.

➤ Encapsulation also simplifies the debugging and program maintenance task .

How to achieve Encapsulation in c#

➤ Defining the class data members as private, thereby making sure that data members of the class are not directly accessible from an object instance.

➤ Accessing the private members through the class methods or by using properties . The Properties use a pair of public accessor (get) and mutator (set) methods to manipulate private data members of a class.

Properties and Encapsulation

Now We have a basic understanding of “Encapsulation“.

The meaning of Encapsulation, is to make sure that “sensitive” data is hidden from users. To achieve this, you must:

  • declare fields/variables as private
  • provide public get and set methods, through properties, to access and update the value of a private field

Properties

 private variables can only be accessed within the same class (an outside class has no access to it). However, sometimes we need to access them – and it can be done with properties.

A property is like a combination of a variable and a method, and it has two methods: a get and a set method:

Example

class Person
{
  private string name; // field

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

Example explained

The Name property is associated with the name field. It is a good practice to use the same name for both the property and the private field, but with an uppercase first letter.

The get method returns the value of the variable name.

The set method assigns a value to the name variable. The value keyword represents the value we assign to the property.

Now we can use the Name property to access and update the private field of the Person class:

Example

class Person
{
  private string name; // field
  public string Name   // property
  {
    get { return name; }
    set { name = value; }
  }
}

class Program
{
  static void Main(string[] args)
  {
    Person myObj = new Person();
    myObj.Name = "Liam";
    Console.WriteLine(myObj.Name);
  }
}

The output will be:

Liam

Automatic Properties (Short Hand)

C# also provides a way to use short-hand / automatic properties, where you do not have to define the field for the property, and you only have to write get; and set; inside the property.

The following example will produce the same result as the example above. The only difference is that there is less code:

Example

Using automatic properties:

class Person
{
  public string Name  // property
  { get; set; }
}

class Program
{
  static void Main(string[] args)
  {
    Person myObj = new Person();
    myObj.Name = "Liam";
    Console.WriteLine(myObj.Name);
  }
}

The output will be:

Liam