find and solve || findandsolve.com
Please wait.....

C# Encapsulation

In c# programming language Encapsulation is a process of binding the data members and member functions into a single unit. Encapsulation, the variables or data of a class are hidden from any other class.

A fully encapsulated class has getter and setter functions that are used to read and write data. This class does not allow data access directly in c# programming language.

Encapsulated is also called data-hiding because of the data in a class is hidden from other classes so can accessed only through any member function of own class in which they are declared.

Create Student Class like as given below

  namespace AccessSpecifiers  
    {
    public class Student
    {
        // Creating setter and getter for each property
        public int ID { get; set; }
        public string Name { get; set; }
        public int Code{ get; set; }
    }
   }  


The main class is:

using System;  
namespace AccessSpecifiers
{
    class Program
    {
        static void Main(string[] args)
        {
            Student objStudent= new Student();
            // Setting values to the properties
            objStudent.ID = 205;
            objStudent.Name = "Sylvia Neupane";
            objStudent.Code=2541784;
            // getting values
            Console.WriteLine("Student ID = "+objStudent.ID);
            Console.WriteLine("Student Name = "+objStudent.Name);
            Console.WriteLine("Stuent Code= "+objStudent.Code);
        }
    }  

Output

Student ID =205

Student Name =Sylvia Neupane

Student Code =2541784