SoFunction
Updated on 2025-03-08

About the encapsulation details of C# class

1. Preface

access data. In addition, you can also control the access method of data. In object-oriented programming, most of them use classes as the basic unit of data encapsulation. Classes combine data and methods of operating data into one unit. When designing a class, you do not want to directly access the data in the class, but rather to access the data through methods. This will achieve the purpose of encapsulating data, which will facilitate future maintenance and upgrades, and you can also make an extra layer of judgment when operating data.

In addition, encapsulation can also solve the permissions of data access. Encapsulation can be used to hide the data to form a closed question, and then it can be set which data can only be used in this space and which data can be used outside the space. A class contains sensitive data that some people can access, and some people cannot access. If access to this data is not restricted, the consequences will be very severe. When writing programs, different access modifiers should be used for the members of the class to define their access levels.

The purpose of encapsulation is to enhance security and simplify programming. Users do not have to understand specific implementation details, but simply use members of the class through the specific access permission of the external interface. For example, a charger, it uses a wire to connect the 220V power supply to the battery after buck-revisation filtering, and then charges it. The process of step-down rectification filtering is equivalent to a class of packaging.

2. Example

【Example 1】Create a console application where you customize oneMyClassClass, which is used to encapsulate the attributes of add and added numbers. Then customize oneAddMethod, which is used to return two of the classint The sum of attributes.Program In the main program class, the objects of the custom class are instantiated and theMyClassAssign values ​​to two attributes in the class. Last callMyClass Custom methods in class Add returns the sum of two properties.

The code is as follows:

class MyClass //Custom class, encapsulate add and add the attributes{
prvate intx=0: //Define an int-type variable as an adderprivate int y = 0; //Define an int variable as added///<summary> 
///Add number///<Summry> 
publie int x
{
get {return x; }
set
{
x =value;
}
}
public int y //Addedget
{
return y;
}
set
{
y * value;
}
}


public int Add() //Sum{
retum x+Y
}
class Program
{
static void Main(string(] args)
{
MyClass myclass u new MyClass(); //Instantiate the object of MyClass=3; //Default value for attributes in MyClass class =5; // Assign values ​​to attributes in MyClass class(()): //Calling the Add method in the MyClass class to sum();
}
}

The result of the operation is 8

This is the end of this article about the details of the C# class encapsulation. For more relevant C# class encapsulation content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!