C Sharp Encapsulation

C# Encapsulation

The concept of wrapping data (data members and member functions) into a single unit called class, to prevent alteration of data from outside is called encapsulation in C#. To access or read such data in a fully encapsulated class, the getter functions of the class are used and the setter functions are used to write data because the data access is not allowed directly.


using System;  
namespace AccessSpecifiers  
    class Employee  
        public string Id { get; set; }  
        public string Name { get; set; }  
        public string City { get; set; }  
    class Details  
        static void Main(string[] args)  
            Employee employee = new Employee();  
            employee.Id = "10";  
            employee.Name = "Vishal Gupta";  
            employee.City = "Delhi";  
            Console.WriteLine("Employee Id: "+ employee.Id);  
            Console.WriteLine("Employee Name: "+ employee.Name);  
            Console.WriteLine("Employee City: "+ employee.City);  



In the above example, we are creating a class to encapsulate the properties. The getter and setter functions are also provided by the class.

Please follow and like us:
Content Protection by DMCA.com