Object-Oriented Programming (OOP) is a programming paradigm that organizes software around objects, rather than actions and logic.
In C#, OOP enables developers to create reusable, modular, and maintainable code using real-world modeling.
💡 Why Use OOP in C#?
- Makes code reusable via inheritance
- Simplifies maintenance
- Encourages modularity
- Enables data hiding and security
- Is the foundation for most C# applications
🧱 The 4 Pillars of OOP in C#
1️⃣ Encapsulation 🔐
Encapsulation is the concept of wrapping data (fields) and behavior (methods) into a single unit: a class.
It also means hiding internal details using access modifiers.
Example:
public class BankAccount
{
private decimal balance;
public void Deposit(decimal amount)
{
if (amount > 0) balance += amount;
}
public decimal GetBalance() => balance;
}
✅ balance is private, only accessible via public methods.
2️⃣ Inheritance 👨👧
Inheritance allows one class to reuse the members of another class.
In C#, inheritance is achieved using the: symbol.
Example:
public class Vehicle
{
public string Brand = "Ford";
public void Honk() => Console.WriteLine("Beep!");
}
public class Car : Vehicle
{
public string Model = "Mustang";
}
✅ Car inherits Vehicle properties and methods.
3️⃣ Polymorphism 🌀
Polymorphism means “many forms” — a method can behave differently based on the object that invokes it.
Two types:
- Compile-time (Method Overloading)
- Runtime (Method Overriding)
Example:
public class Animal
{
public virtual void Speak() => Console.WriteLine("Animal sound");
}
public class Dog : Animal
{
public override void Speak() => Console.WriteLine("Bark!");
}
✅ Even though Dog is an Animal, it overrides Speak().
4️⃣ Abstraction 🧊
Abstraction means hiding the complex implementation and showing only essential details.
Ways to achieve abstraction in C#:
- Abstract classes
- Interfaces
Example:
public interface IShape
{
double Area();
}
public class Circle : IShape
{
public double Radius { get; set; }
public double Area() => Math.PI * Radius * Radius;
}
✅ The caller just calls.Area()
— no need to know the formula inside.
🚀 Real-Life Example: A Simple E-Commerce System
public abstract class Product
{
public string Name { get; set; }
public abstract decimal GetPrice();
}
public class Book : Product
{
public decimal Price { get; set; }
public override decimal GetPrice() => Price;
}
public class Order
{
public List<Product> Items = new List<Product>();
public decimal Total()
{
return Items.Sum(item => item.GetPrice());
}
}
This example shows:
- Abstraction via Product
- Inheritance via Book : Product
- Polymorphism in GetPrice()
- Encapsulation in Order
🔁 Best Practices for OOP in C#
✅ Use meaningful class names
✅ Keep fields private and use properties
✅ Favor composition over inheritance where possible
✅ Keep your classes single-responsibility
✅ Interface-driven design for flexibility
🧩 When NOT to Use OOP
- When performance is critical and overhead must be minimal
- For very small scripts or procedural code with no reuse
✅ Final Thoughts
OOP is the heartbeat of C#.
If you understand the 4 pillars, you unlock the power to write robust, reusable, scalable, and clean code, whether you’re building a console app, ASP.NET Core API, or a Unity game.