C# Encapsulation with Examples

In c#, Encapsulation is a process of binding the data members and member functions into a single unit. In c#, class is the real time example for encapsulation because it will combine a various type of data members and member functions into a single unit.

 

Generally, in c# the encapsulation is used to prevent an alteration of code (data) accidently from the outside of functions. In c#, by defining the class fields with properties we can protect the data from accidental corruption.

 

If we define a class fields with properties, then the encapsulated class won’t allow us to access the fields directly, instead we need to use getter and setter functions to read or write a data based on our requirements.

 

Following is the example of defining an encapsulation class using properties with get and set accessors.

 

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;

            }

        }

    }

If you observe 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 then defined variables, then we can make it by using properties with get and set accessors.

C# Encapsulation Example

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

 

using System;

using System.Text;

 

namespace Tutlane

{

    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 Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we defined a fields in encapsulated class using properties and we are able to manipulate field values using get and set accessors of properties.

 

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

 

C# Encapsulation Example Result

 

This is how we can use encapsulation in c# programming language to bind data members and member functions into single unit by protecting the data from accidental corruption.