For a more in-depth understanding of encapsulation in C#, readers are encouraged to refer to this Mytour article.
Encapsulation in C#
1. Encapsulation in C#.
2. Using Accessor and Mutator in Encapsulation.
3. Utilizing Properties in Encapsulation.
3.1. Read-only Property.
3.2. Write-only Property.
1. Encapsulation in C#
In C#, Encapsulation is the process of combining member data and member functions into a single unit. A class in C# exemplifies Encapsulation, as it combines various member data and member functions into a unit.
Fundamentally, Encapsulation in C# is used to prevent unintentional modification of code (data) from outside functions. By defining classes with properties in C#, data is safeguarded against accidental corruption.
2. Encapsulation Using Accessor and Mutator
If classes are defined with properties, they become encapsulated, preventing direct access to fields. Instead, users must utilize get and set functions to read or write data as per requirements.
Here is an example of defining an encapsulated class using properties with get and set accessors:
In the code snippet above, you can observe defining a variable with private access modifier and exposing those variables publicly using get accessor and set accessor properties.
If you wish to make any modifications, define variables using get accessor and set accessor properties.
3. Encapsulation Using Properties
Properties are a novel language feature introduced in C#. Only a handful of languages support this feature. In C#, properties help secure a field within a class through reading and writing to that field. Encapsulation can be more smoothly enforced with properties.
To gain a deeper understanding, readers are encouraged to explore the following example:
The example above illustrates the application of Encapsulation using properties. Properties comprise get accessor and set accessor. The get accessor returns the value of property fields, while the set accessor sets the values of property fields with the content of the given value. Properties can be set to read-only, achieved by implementing only the get accessor in the property.
3.1. Read-Only Property
In the above example, it demonstrates the implementation of a read-only property. The ReadDepartment class includes the Departname property, implementing only the get accessor and retaining the set accessor. This specialized class includes a constructor that accepts a string parameter. The Main method of the ReadDepartmain class creates a new object named d.
The initialization of object d utilizes the constructor of ReadDepartment, taking a string parameter. Since the program is read-only, we cannot set the value for the Departname field. It allows only reading or retrieving values from the field.
Furthermore, properties can also be designed for write-only operations by implementing only the set accessor within the property.
3.2. Write-Only Property
In the example above, it illustrates the implementation of a write-only property. The WriteDepartment class includes the Departname property, implementing only the set accessor while retaining the get accessor. The set accessor method is slightly modified as it prints the value of Departname after being assigned.
Thus, this article by Mytour has just introduced you to encapsulation in C#. If you have any queries or questions, please feel free to leave your comments below the article, and Mytour will address your concerns as soon as possible. Additionally, readers can explore more articles on Mytour to gain a deeper understanding of loops in C#.