Lesson 29 +10 XP

C# OOP & Classes

C# OOP & Classes

C# is an object-oriented language. Everything revolves around classes and objects.

OOP stands for Object-Oriented Programming

OOP organizes code around objects - things that have data (fields) and behavior (methods). C# is deeply OOP: even Main lives inside a class.

Benefits of OOP

  • Faster and easier to execute programs.
  • Clear structure - code is organized around real-world things.
  • Reuse - classes are blueprints used again and again.
  • Maintainability - easier to update and debug.

Classes and Objects

  • A class is a blueprint or template.
  • An object is an actual instance created from that blueprint.

Think of a Car class as the blueprint and a specific myCar as the object built from it.

Classes and Objects

class Car
{
  string color = "red";
}

static void Main(string[] args)
{
  Car myObj = new Car();
  Console.WriteLine(myObj.color);
}

new Car() creates an object. myObj.color reads its field.

The four pillars of OOP

  1. Encapsulation - keep data safe inside an object (access modifiers, properties).
  2. Inheritance - build new classes from existing ones.
  3. Polymorphism - same method name, different behavior.
  4. Abstraction - hide complexity, show only what matters.

TL;DR

  • A class is a blueprint; an object is an instance of it.
  • OOP = objects with data and behavior.
  • Pillars: encapsulation, inheritance, polymorphism, abstraction.
  • new ClassName() creates an object.