Build smarter, reusable, and scalable software
Object-Oriented Programming (OOP) is one of the most powerful programming paradigms in C#. It’s what makes large applications easier to manage and lets developers model real-world problems using code.
In this guide, you’ll learn the core principles of OOP in C#, how to use classes, objects, inheritance, encapsulation, and more — with clear examples and best practices.
🧱 What is Object-Oriented Programming?
OOP is a way of programming that models real-world things as objects in code.
Instead of writing everything in one big program, you divide your code into objects that:
- Have data (called fields or properties)
- Perform actions (called methods)
- Belong to classes (templates for creating objects)
Think of a class like a blueprint, and an object like the house built from it.
🧑🎓 A Simple Example
Let’s model a Car using a class:
public class Car
{
// Properties (data)
public string Make;
public string Model;
public int Year;
// Method (behavior)
public void Honk()
{
Console.WriteLine("Beep beep!");
}
}
To use this class:
Car myCar = new Car();
myCar.Make = "Toyota";
myCar.Model = "Corolla";
myCar.Year = 2020;
myCar.Honk(); // Output: Beep beep!
🔍 Key OOP Concepts in C#
| Concept | Description |
|---|---|
| Class | A blueprint for objects (e.g., Car) |
| Object | A real instance of a class |
| Property/Field | Stores data about the object |
| Method | Performs an action |
| Encapsulation | Hiding internal data and exposing only what’s necessary |
| Inheritance | Letting one class use the code of another |
| Polymorphism | Ability to use the same method name differently in different classes |
🧳 Encapsulation
Encapsulation protects data by using private fields and public methods (getters and setters):
public class BankAccount
{
private decimal balance = 0;
public void Deposit(decimal amount)
{
if (amount > 0)
balance += amount;
}
public decimal GetBalance()
{
return balance;
}
}
This ensures only safe interactions with the
balancefield.
🧬 Inheritance
Inheritance lets you create a new class that builds on another.
public class Animal
{
public void Eat() => Console.WriteLine("Eating...");
}
public class Dog : Animal
{
public void Bark() => Console.WriteLine("Woof!");
}
Dog myDog = new Dog();
myDog.Eat(); // From Animal
myDog.Bark(); // From Dog
Doginherits everything fromAnimal.
🔁 Polymorphism
Polymorphism allows different classes to have methods with the same name but different behavior.
public class Animal
{
public virtual void Speak() => Console.WriteLine("The animal makes a sound.");
}
public class Cat : Animal
{
public override void Speak() => Console.WriteLine("Meow.");
}
public class Dog : Animal
{
public override void Speak() => Console.WriteLine("Woof.");
}
List<Animal> animals = new List<Animal>
{
new Cat(),
new Dog()
};
foreach (Animal a in animals)
{
a.Speak(); // Each animal speaks in its own way
}
🛠 Real-World Analogy
Let’s say you’re building a school management app:
| Class | Properties | Methods |
|---|---|---|
Student | Name, Age, Grade | Register(), Study() |
Teacher | Name, Subject, Room | Teach(), GradePapers() |
Person | Name, Age (base class) | Speak(), Sleep() |
Student and Teacher can inherit from Person, share common features, and define their own.
📚 Summary Table
| Term | Example | Description |
|---|---|---|
| Class | Car, Student, Dog | Blueprint for objects |
| Object | new Car() | A real thing |
| Method | Drive(), Honk() | Action performed |
| Property | Make, Model, Year | Data stored in object |
| Inheritance | Dog : Animal | Reuse code from base class |
| Encapsulation | Private fields + public methods | Controls access to data |
| Polymorphism | Speak() behaves differently | Flexible and extendable behavior |
🧪 Quick Practice Challenge
- Create a class called
Bookwith:Title,Author,Pagesas properties- A method
GetSummary()that prints “Title by Author”
- Create another class
EBookthat inherits fromBookand adds:- A
FileSizeproperty - An overridden
GetSummary()method that prints the file size
- A
📬 Want to Go Deeper?
OOP is essential in desktop apps, APIs, games, and enterprise software — and now you’re on your way to mastering it.
Take our Object-Oriented C# with LINQ and Entity Framework Course