Introduction to Object-Oriented Programming (OOP) in C#

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#

ConceptDescription
ClassA blueprint for objects (e.g., Car)
ObjectA real instance of a class
Property/FieldStores data about the object
MethodPerforms an action
EncapsulationHiding internal data and exposing only what’s necessary
InheritanceLetting one class use the code of another
PolymorphismAbility 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 balance field.


🧬 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

Dog inherits everything from Animal.


🔁 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:

ClassPropertiesMethods
StudentName, Age, GradeRegister(), Study()
TeacherName, Subject, RoomTeach(), GradePapers()
PersonName, Age (base class)Speak(), Sleep()

Student and Teacher can inherit from Person, share common features, and define their own.


📚 Summary Table

TermExampleDescription
ClassCar, Student, DogBlueprint for objects
Objectnew Car()A real thing
MethodDrive(), Honk()Action performed
PropertyMake, Model, YearData stored in object
InheritanceDog : AnimalReuse code from base class
EncapsulationPrivate fields + public methodsControls access to data
PolymorphismSpeak() behaves differentlyFlexible and extendable behavior

🧪 Quick Practice Challenge

  1. Create a class called Book with:
    • Title, Author, Pages as properties
    • A method GetSummary() that prints “Title by Author”
  2. Create another class EBook that inherits from Book and adds:
    • A FileSize property
    • An overridden GetSummary() method that prints the file size

📬 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