Encapsulation in C#


Encapsulation

Encapsulation is defined as the accumulation of data under one unit. It is a method that binds the code and the data it uses. Alternatively, wrapping is a protection that protects data from being accessed by a code outside this shield.

Technically in compilation, dynamics or category data is hidden from any other class and can only be accessed by any member of the class category where it is announced. As with encapsulation, class data is hidden in other classes, which is also known as data encryption.

Integration can now be achieved: All class variables are declared private, and C-Class Structures are used to set and retrieve variable values.

// // # program to show wrap using the System; using System;    public class DemoEncap { private String studentName; private int int; // uses access to retrieve // set StudentName value public name String { find out { return student } set { readerName = value; } } // uses access to retrieve // set the StudentAge value public int Age { find out { return studentAge; } set { readerAge = value; } } } // Driving Category section GFG { // Main Method static public void Main () { // create an object DemoEncap obj = new DemoEncap (); // phones set the device name scanner, // and pass "Ankita" as the value of // standard field value obj.Name = "Ankita"; // calls setting Age Age access, // and pass "21" as the value of // standard field value Age = 21; // Display variable values Console.WriteLine ("Name:" + obj.Name); Console.WriteLine ("Age:" + obj.Age); } }

Output:

Name: Ankita

Age: 21