find and solve ||
Please wait.....

Encapsulation Example In C#


     Object-Oriented Programming Concepts

Object oriented programmming(OOP) is a programming language where programs are organized around objest ato action and logic.
This is  the most popular programming languages in c# including abastraction,encapsulation,inheritance and polymorphism.

All the programming languages supporting Object Oriented Programming will be supporting main concepts given below, 


Encapsulation is hide data.also 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 object.This is wrapping up of data under a single unit.This is the procedure of encapsulating data and functions into a single unit.It collects data members and member function into a single unit called class.



There are some important Encapsulation.

⦁    Encapsulation is also called "data-hiding" because encapsulation,the data in a class is hidden from other classes.
⦁    Hiding Type Members with Private Access.
⦁    Re-usability and easy to change with new requirements.
⦁    The code is very easy to test for unit testing.
⦁    Increased Flexibility because  the variables of the class as read-only or write-only depending on our project or requirement.
⦁    So readonly to make Get Accessor in the code. And write-only then make only use Set Accessor.
⦁    Your objects are able to manipulate the internal data point using a single named item.


C# Encapsulation Example


namespace Example
        class Employee
            // Creating setter and getter for each property  
            public string EmployeeId { getset; }
            public string Name { get; set; }
            public string Email { getset; }

    using System;  
    namespace Example
        class Program
            static void Main(string[] args)
                Employee emp = new Employee();
                // Setting values to the properties  
                emp.EmployeeId = "120";
                emp.Name = "Devin";
                emp.Email = "[email protected]";
                // getting values  
                Console.WriteLine("EmployeeId = " + emp.EmployeeId);
                Console.WriteLine("Name = " + emp.Name);
                Console.WriteLine("Email = " + emp.Email);


EmployeeId= 120
Name = Devin
Email = [email protected]



Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.


Report Response