Module 3 - Chapter 5: Encapsulation in C#

Encapsulation is a fundamental concept in object-oriented programming (OOP) that involves bundling the data (attributes) and methods (functions) that operate on the data into a single unit, known as a class. It also restricts direct access to some of the object's components, which is a way of preventing accidental manipulation of data and ensuring internal data integrity. This principle of hiding the internal state and requiring all interaction to occur through an object's methods is central to C#. This chapter explains the concept of encapsulation and demonstrates how to implement it in C# through access modifiers and properties.

Mar 29, 2024

Encapsulation in C#

Encapsulation is a fundamental concept in object-oriented programming (OOP) that involves bundling the data (attributes) and methods (functions) that operate on the data into a single unit, known as a class. It also restricts direct access to some of the object's components, which is a way of preventing accidental manipulation of data and ensuring internal data integrity. This principle of hiding the internal state and requiring all interaction to occur through an object's methods is central to C#. This chapter explains the concept of encapsulation and demonstrates how to implement it in C# through access modifiers and properties.

Why Encapsulation?

Encapsulation provides two main benefits:
  • Security: By hiding the internal state of objects, only allowed operations can be performed, reducing unintended or harmful modifications.
  • Simplicity: Encapsulation simplifies the usage of objects by separating the interface from the implementation. Users of a class do not need to understand its inner workings to use it.

Implementing Encapsulation in C#

Encapsulation in C# is primarily achieved using access modifiers and properties.

Access Modifiers

Access modifiers control the visibility of class members (fields, properties, methods) to other parts of the code. The most common access modifiers are:
  • public: Members are accessible from any part of the code.
  • private: Members are only accessible within the same class.
  • protected: Members are accessible within the same class and by derived class instances.

Using Properties for Encapsulation

Properties in C# are a way to control access to class data. A property is defined using get and set accessors. The get accessor is used to return the property value, and the set accessor is used to assign a new value. These accessors can have different access levels.

Example of Encapsulation with a Property

public class Person { private string name; // Private field // Public property public string Name { get { return name; } set { if (!string.IsNullOrEmpty(value)) { name = value; } else { Console.WriteLine("Name cannot be null or empty."); } } } }
In this example, the name field is made private, meaning it cannot be accessed directly from outside the Person class. The Name property allows controlled access to the name field, including validation within the set accessor to ensure the name is not set to a null or empty string.

Auto-Implemented Properties

C# provides a shorthand syntax for properties that do not require additional logic in the get and set accessors, known as auto-implemented properties.
public class Employee { public int Id { get; set; } public string Name { get; set; } }
Here, Id and Name are public properties with default get and set accessors. Behind the scenes, C# compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.

Summary

Encapsulation is a cornerstone of OOP that enhances security, simplicity, and integrity of code. In C#, encapsulation is implemented through the use of access modifiers and properties, allowing you to control how the class data is accessed and modified. Proper use of encapsulation makes your C# classes easier to use and maintain, while also protecting their internal state from improper or unintended use.