Encapsulation in C# is defined as a built-in C# programming language functionality to fulfill the functionality of encapsulation. To understand the encapsulation functionality, it is defined as wrapping up of one or more items to a single unit and make sure that they have logical or physical similarity, which makes a reason to package them together. They are packaged together to ensure that access to implementation details is prevented, and by doing this, the alteration to the data is prevented. The only way the details can be accessed is through the getter function in the class, and the only way to modify the details is through setter functions. The packaged entity which has the collected data members and member functions into a single entity is known as a class.
Syntax of Encapsulation C#
The idea of having sprintf started gaining popularity when it became an alternative approach to look at the storing the printable messages instead of printing them in the console so that they can be referred to anytime as per the need of the application. The sprintf stands for “String Print”, and here we will look at the syntax perspective of the sprintf function in C and understand the parameters or arguments in the syntax.
1. Declaration of the variable to be encapsulated in C#.
private < data type > < variable name >;
2. Getter function of the variable, which is encapsulated in C#.
get { return < variable name >; }
3. Setter function of the variable, which is encapsulated in C#.
set { return < variable name >; }
4. Declaration of the class that will encapsulate in C#.
public class < class name >{ // Declare the elements of the public class. }
Given below are the examples of Encapsulation C#:
Trying to access a private variable in the class using accessors & mutators (ERROR expected in this code).
Code:
using System; namespace EmployeeApplication { class Employee { private string name; private string dept; public string GetName() { return name; } public void SetName(string n) { name = n; } public string GetDept() { return name; } public void SetDepartname(string d) { dept = d; } public void Display() { Console.WriteLine("Name: {0}", name); Console.WriteLine("Department: {0}", dept); } } class ExecuteRectangle { static void Main(string[] args) { Employee e = new Employee(); e.name = "AmKy"; e.dept = "EduCBA"; e.Display(); Console.ReadLine(); } } }
Output:
Using the proper accessors and mutators genre to access and modify elements of the class.
Code:
using System; namespace EmployeeApplication { class Employee { private string name; private string dept; public string GetName() { return name; } public void SetName(string n) { name = n; } public string GetDept() { return name; } public void SetDepartname(string d) { dept = d; } public void Display() { Console.WriteLine("Name: {0}", name); Console.WriteLine("Department: {0}", dept); } } class ExecuteRectangle { static void Main(string[] args) { Employee e = new Employee(); e.SetName("AmKy"); e.SetDepartname("EduCBA"); e.Display(); Console.ReadLine(); } } }
Output:
Encapsulation using properties.
Code:
using System; namespace EmployeeApplication { class Employee { private string name; private string dept; public string DeptProp { get { return dept; } set { dept = value; } } public string EmpName { get { return name; } set { name = value; } } public void Display() { Console.WriteLine("Name: {0}", name); Console.WriteLine("Department: {0}", dept); } } class ExecuteRectangle { static void Main(string[] args) { Employee e = new Employee(); e.EmpName = "Employee 2"; e.DeptProp = "Finance"; e.Display(); Console.ReadLine(); } } }
Output:
In this article, we have gone through the ways how encapsulation is done in C# along with examples where in we also understood how accessing any private object from other class, even through the instance of the parent class, can lead to error and making the code non-runnable.
The above is the detailed content of Encapsulation C#. For more information, please follow other related articles on the PHP Chinese website!