Lesson 1: Introduction to OOP & Classes in C#
Object-Oriented Programming (OOP) is the dominant paradigm in C# development. Instead of writing a long list of instructions, OOP lets you model real-world entities as objects — each bundling its own data and behavior. This lesson introduces the core philosophy, the four pillars, and how to write your first class.
What Is OOP?
OOP is a way of organizing code around objects — self-contained units that combine state (data) and behavior (methods). The real world is already full of objects: a car has a color and speed (state) and can accelerate or brake (behavior). OOP lets you model these directly in code.
C# is an object-oriented language at its core. Even primitive operations like Console.WriteLine() involve calling a method on a class.
The Four Pillars of OOP
🏠 Encapsulation
Bundling data and methods together, hiding internal details from the outside world. Controls what can be accessed.
🧬 Inheritance
A class (child) can inherit fields and methods from another class (parent), promoting code reuse and hierarchy.
🎭 Polymorphism
The same method can behave differently depending on which object calls it — "many forms" of the same interface.
🎨 Abstraction
Exposing only what's necessary, hiding complexity. Users interact with a simple interface without knowing the internals.
Classes vs Objects
A class is a blueprint. An object is an instance created from that blueprint. Think of a class as the cookie cutter and objects as the actual cookies:
Defining Your First Class
In C#, a class is defined with the class keyword. By convention, class names use PascalCase:
// Define a class called Car
public class Car
{
// Fields — the data (state) this class holds
public string Make;
public string Color;
public int Speed;
// Method — the behavior this class can perform
public void Accelerate(int amount)
{
Speed += amount;
Console.WriteLine($"{Make} is now going {Speed} km/h");
}
public void Describe()
{
Console.WriteLine($"A {Color} {Make} at {Speed} km/h");
}
}
Creating Objects (Instances)
Use the new keyword to create an object from a class. Each object gets its own copy of the class's fields:
// Create two separate Car objects
Car myCar = new Car();
myCar.Make = "Toyota";
myCar.Color = "Red";
myCar.Speed = 0;
Car yourCar = new Car();
yourCar.Make = "Honda";
yourCar.Color = "Blue";
yourCar.Speed = 60;
// Call methods on each object independently
myCar.Accelerate(80); // Toyota is now going 80 km/h
yourCar.Describe(); // A Blue Honda at 60 km/h
// C# 9+ target-typed new expression (shorter syntax)
Car taxiCar = new();
taxiCar.Make = "Ford";
💡 Memory: Value types (int, bool) are stored on the stack. Objects (class instances) are stored on the heap — your variable holds a reference (address) to where the object lives. Two variables can reference the same object.
Reference vs Value — A Key Distinction
// REFERENCE TYPE — both variables point to the SAME object
Car carA = new Car() { Make = "BMW", Speed = 0 };
Car carB = carA; // carB is NOT a copy — it's the same object!
carB.Speed = 200;
Console.WriteLine(carA.Speed); // Output: 200 ← carA changed too!
// VALUE TYPE — each variable is independent
int x = 10;
int y = x; // y is a copy
y = 99;
Console.WriteLine(x); // Output: 10 ← x unchanged
Object Initializer Syntax
C# provides a clean shorthand for setting multiple fields at creation time:
// Object initializer — sets fields inline using { }
Car sportsCar = new Car
{
Make = "Ferrari",
Color = "Red",
Speed = 0
};
sportsCar.Accelerate(250); // Ferrari is now going 250 km/h
🧠 Quick Check — Lesson 1
In C#, what is the difference between a class and an object?
Lesson Summary
OOP organizes code around objects — units combining state (fields) and behavior (methods).
The four pillars are Encapsulation, Inheritance, Polymorphism, and Abstraction.
A class is the blueprint; an object is a specific instance created from it using new.
Objects are reference types — assigning one object variable to another makes them point to the same data.
Use object initializer syntax new Car { Make = "X" } for clean, inline object setup.