Home > Backend Development > C#.Net Tutorial > Encapsulation C#

Encapsulation C#

WBOY
Release: 2024-09-03 15:05:35
Original
585 people have browsed it

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 >;
Copy after login

2. Getter function of the variable, which is encapsulated in C#.

get
{
return < variable name >;
}
Copy after login

3. Setter function of the variable, which is encapsulated in C#.

set
{
return < variable name >;
}
Copy after login

4. Declaration of the class that will encapsulate in C#.

public class < class name >{
// Declare the elements of the public class.
}
Copy after login

How does Encapsulation work in C#?

  • The concept of encapsulation came into being when data in an application was prone to getting corrupted due to non-noticeable errors that might happen by any other person. Since data is a critical element in an Object-Oriented Programming concept, we need to avoid any accidental modification to the data by any outside function and, in the course, save the data from any malicious attacks as well.
  • The way the data is encapsulated is by defining the elements to be private and making them so allows the modification possible in only 2 ways. These are the 2 ways we will talk about in detail now to get a complete picture of the working of encapsulation. The first way is by accessors and mutators. When a class is made, we make some elements private, which means that those elements have no access from elements outside the class. Now, we make 2 functions which makes sure that if any modification or even retrieval needs to be done, they will be done by accessors (for retrieval) and mutators (for modification). These functions are what is defined as public so that any function/class outside the native class can access the mutators and accessors. The other way is by using properties, and there are 2 properties get and set that allows retrieval and modification of elements, respectively.
  • From these 2 ways, when the desired accessor is called in another class by defining an instance of the class which contain the private elements. This instance, by default, includes all the public functions of the parent class (the class to whom the instance belongs to). Now using that public properties/accessors/mutators, the required task is called for. After being called, the corresponding task is assigned of either retrieval or modification as per the choice the task is done. This working can be thought to be analogous to an organization that only some people in the organization have access to the financial records (the most confidential information), and if one needs to retrieve or modify any entry, the instance of the finance department is called and assigned the corresponding job.

Examples of Encapsulation C#

Given below are the examples of Encapsulation C#:

Example #1

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();
}
}
}
Copy after login

Output:

Encapsulation C#

Example #2

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();
}
}
}
Copy after login

Output:

Encapsulation C#

Example #3

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();
}
}
}
Copy after login

Output:

Encapsulation C#

Conclusion

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!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template