What is Encapsulation in C#

What is Encapsulation in C#

Encapsulation is in c# refers to an object’s ability to hide data and behavior that are not necessary to its user. Encapsulation enables a group of properties, methods and other members to be considered a single unit or objec, Encapsulation is also known as information hiding.

Encapsulation is used to restrict access to the members of a class so as to prevent the user of a given class from manipulating objects in ways that are not intended by the designer. While encapsulation hides the internal implementation of the functionalities of class without affecting the overall functioning of the system, it allows the class to service a request for functionality and add or modify its internal structure (data or methods) to suit changing requirements

The following are the benefits of encapsulation

  • Protection of data from accidental corruption
  • Flexibility and extensibility of the code and reduction in complexity
  • Specification of the accessibility of each of the members of a class to the code outside the class
  • Lower coupling between objects and hence improvement in code maintainability

Why do we need 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 in C# is implemented with access modifiers, following access modifier is used with encapsulation

Public:       The members (Functions & Variables) declared as public can be accessed from anywhere
Private:      Private members cannot be accessed from outside the class
Protected:   Protected members can be accessed only from the child classes
Internal:     It can be accessed only within the same assembly
Protected Internal:   It can be accessed within the same assembly as well as in derived class

What is Encapsulation in C# with Example

using System;
namespace OppsConcept
{
    class Program
    {
        static void Main(string[] args)
        {
            User u = new User();
            u.Name = "C# User";
            u.City = "Ahmedabad";
            Console.WriteLine("Name: " + u.Name);
            Console.WriteLine("Location: " + u.City);
            Console.WriteLine("\n  Press Enter Key to Exit..");
            Console.ReadLine();
        }

        class User
        {
            private string city;
            private string name;

            public string City
            {
                get
                {
                    return city;
                }
                set
                {
                    city = value;
                }
            }

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

In above code, we defined a variables with private access modifiers and exposing those variables in public way by using properties get and set accessors. In case, if you want to make any modifications to the defined variables, then we can make it by using properties with get and set accessors.

When we execute above c# program, we will get the result like as shown below.

What is Encapsulation in C#

Difference between Abstraction and Encapsulation

Abstraction Encapsulation
It is used to hide unwanted data and show only required properties and methods. It is used to bind data members and member functions into single unit to prevent outsiders to access it directly

Also Learn More Tutorial :

Facebook Notice for EU! You need to login to view and post FB Comments!

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *