Encapsulation in C#

 Encapsulation in C# is a core concept in object-oriented programming (OOP) that focuses on bundling data (fields) and the methods that operate on that data within a single unit (a class). It also involves controlling access to that data, preventing direct and unauthorized modification.

Key aspects of encapsulation:

  • Data Hiding: Protecting the internal state of an object by making its fields private. This prevents external code from directly accessing and modifying the data, ensuring data integrity.
  • Access Control: Providing controlled access to the data through public methods (getters and setters or properties). This allows you to define rules and logic for how the data can be accessed and modified.
  • Bundling: Combining data and methods within a class, creating a self-contained unit that represents a specific concept or entity.

How encapsulation is achieved in C#:

  • Access Modifiers: C# provides access modifiers to control the visibility and accessibility of class members:
    • private: The member is only accessible within the same class.
    • public: The member is accessible from any other code.
    • protected: The member is accessible within the same class and its derived classes.
    • internal: The member is accessible within the same assembly.
  • Properties: Properties provide a flexible and controlled way to access and modify private fields. They use get and set accessors to define the logic for retrieving and setting the field's value.

Example:

C#
public class Person
{
    private string name; // Private field

    // Public property to access and modify the name field
    public string Name
    {
        get { return name; }
        set
        {
            if (!string.IsNullOrEmpty(value)) // Validation logic
            {
                name = value;
            }
        }
    }

    private int age; // Private field
    public int Age
    {
        get { return age; }
        set
        {
            if (value >= 0 && value <= 150)
            {
                age = value;
            }
        }
    }
}

public class Example
{
    public static void Main(string[] args)
    {
        Person person = new Person();
        person.Name = "John Doe"; // Accessing the property
        person.Age = 30;

        Console.WriteLine("Name: " + person.Name); // Output: John Doe
        Console.WriteLine("Age: " + person.Age); // Output: 30

        person.Age = -5; // Value will not be assigned due to validation
        Console.WriteLine("Age: " + person.Age); // Output: 30
    }
}

Benefits of Encapsulation:

  • Data Protection: Prevents direct and unauthorized access to data, ensuring data integrity and consistency.
  • Code Maintainability: Changes to the internal implementation of a class do not affect external code as long as the public interface (properties and methods) remains the same.
  • Code Reusability: Encapsulated classes can be easily reused in different parts of the application or in other projects.
  • Flexibility: Allows you to add validation logic or other logic within the get and set accessors of properties, providing more control over data access.

Encapsulation is a fundamental principle in C# and other OOP languages that helps in writing robust, maintainable, and reusable code. By bundling data and methods and controlling access to data, you can create well-structured and secure applications.

Comments

Popular posts from this blog

Kotlin Math Operations and Functions Overview

Kotlin Strings: Features and Operations Guide

Kotlin Android Program (QCR) Application Codes That Read Text in Photos