One of the most important concepts in object-oriented programming is encapsulation. Encapsulation is a powerful tool that can help you create robust and maintainable code that protects from unwanted access. In this blog, we will cover what encapsulation is, how to create public, private, and protected members in C#, and the advantages of using encapsulation in your codebase.
What is encapsulation?
Encapsulation is a fundamental principle of object-oriented programming. It's a way of bundling data and protecting that data in an object from being accessed or modified by outside code. Encapsulation is achieved by grouping similar data and methods into one place, i.e. "encapsulating" them in a class and controlling access by using access modifiers with public, private, and protected keywords. By encapsulating data and methods into a class, you can control how other classes interact with a class. This can prevent other classes from accidentally modifying code, and it can also make it easier to change the implementation later on without affecting the rest of the codebase.
Encapsulation is often confused with abstraction. Abstraction is about hiding the implementation details of a class from other classes. Encapsulation is about bundling similar data and methods and hiding the data in an object from being accessed or modified by code outside the object.
Implementing encapsulation in C#
Let's look at how to create public, private, and protected members in C#.
• Public members are accessible from anywhere outside the class.
• Private members are only accessible from within the class.
• Protected members are accessible from within the class and from derived classes.
*Get and Set Methods *
As we mentioned above, one way to achieve encapsulation is by using public methods to get and set the values of private variables. These methods are often called "getters" and "setters".
Let's look at an example. We have a class with two private variables: _name and _age. We've provided public methods to get and set the values of these variables.

You might be wondering why we need set and get methods at all. Can't we just make the variables public? The answer is no. If we made the variables public, anyone could access and modify them from outside the class, violating the principle of encapsulation.
Notice that the setters have a void return type and take one parameter of the same type as the variable being set. The getters have a return type of the same type as the variable being returned and don't take any parameters. This is a common pattern, but it's not required. You can name your methods whatever you want and have them return whatever type you want. The only requirement is that the setter method must take one parameter, and the getter method must not take any parameters.
*Properties *
A shortcut way of get and set in C# is using properties.
A property is a public member of a class that encapsulates a private field. A property has a getter and setter method to access the field. The getter method returns the value of the field, and the setter method sets the value of the field.
You can create a property by using the following syntax:

The advantages of encapsulation
Data security: A significant advantage of encapsulation is that is provides a way to protect the data in an object from being accessed or modified by code outside the object. This is helpful because it keeps the data safe from accidentally corrupted by other code or users.
Code is easier to maintain: When a class is encapsulated, it is easier to see what it does and how it works. All the data and methods are in one place, so it is easy to find them when you need to make changes.
Encapsulation also makes code easier to read and understand: you can see at a glance what data the class is working with and what methods are available to manipulate that data. This can make your codebase more maintainable in the long run.
other classes need to be able to access, you would make it public. Similarly, if you have a method that other classes need to be able to call, you would make it public.
Encapsulation is a powerful tool that can help you to protect your data and make your code easier to maintain. It's not something you should use all the time, but it can be very useful in certain situations. I hope this article has been helpful and given you a better understanding of what encapsulation is and how to use it in your own projects. I am a student learning object-orientated programming (OOP) so if you have any comments or suggestions, please feel free to leave them below. Thanks for reading!
Top comments (0)