- C# Fundamentals Tutorial
- C# - Intro
- C# - Installation
- C# - Basic Syntax
- C# - Variables
- C# - Data Types
- C# - Operators
- C# - Type Conversion
- C# - Arrays
- C# - Strings
- C# - Methods
- C# - Classes
- C# - If..Else
- C# - Loops
- C# - Enums
- C# - Inheritance
- C# - Encapsulation
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Regular Expressions
- C# - Exception Handling
C# - Encapsulation
Encapsulation is one of the fundamental principles of object-oriented programming (OOP), and it plays a crucial role in C#. Encapsulation refers to the bundling of data (attributes) and the methods (functions) that operate on the data into a single unit known as a class. It helps in hiding the internal implementation details of a class and exposing only what is necessary for the outside world. The main goals of encapsulation are to achieve data abstraction, data hiding, and code organization.
Here are some key aspects of encapsulation in C#:
Encapsulation involves using access modifiers like private, protected, internal, and public to control the visibility of class members.
Members marked as private are accessible only within the same class.
public class MyClass
{
private int myPrivateVariable;
private void MyPrivateMethod()
{
// Implementation details hidden from outside
}
}
Properties and Accessors:
Encapsulation often includes the use of properties to provide controlled access to the private fields of a class.
Properties encapsulate the logic for getting and setting values.
public class Person
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
Methods as Interface to Data:
Encapsulation allows methods to act as an interface to the internal data of a class.
Methods can perform validation, calculations, and other operations to ensure the integrity of the encapsulated data.
public class BankAccount
{
private decimal balance;
public void Deposit(decimal amount)
{
// Validation logic can be added here
balance += amount;
}
public void Withdraw(decimal amount)
{
// Validation logic can be added here
balance -= amount;
}
}
Getters and Setters:
Encapsulation often involves providing getters and setters to control access to the private members.
public class Temperature
{
private double celsius;
public double Celsius
{
get { return celsius; }
set { celsius = value; }
}
}
Encapsulation and Abstraction:
Encapsulation is closely related to the concept of abstraction, where a class exposes a simple and clear interface to the outside world while hiding its internal complexity.
Abstraction allows users of the class to focus on what the class does rather than how it does it.
public class Car
{
private Engine engine;
public void Start()
{
// Abstraction of internal details
engine.Start();
}
}
Encapsulation promotes modularity, reusability, and maintainability by preventing direct access to the internal details of a class. It also allows for better control over the class's behavior and ensures that the class's state remains consistent.