TExcelTAD
Home › C# › C# OOP

C# OOP

C# is an object-oriented language. Object-oriented programming (OOP) organises a program around objects — bundles of data and the behaviour that works on that data — instead of around a long list of procedures.

Procedural versus object-oriented

In procedural code, data and functions live separately. You pass data around to functions that act on it:

csharp
double balance = 100;
balance = Deposit(balance, 50);
balance = Withdraw(balance, 30);

In object-oriented code, the data and the operations that belong to it are packaged together in a class. The object is responsible for its own state:

csharp
var account = new BankAccount(100);
account.Deposit(50);
account.Withdraw(30);

The second version reads more naturally, and the BankAccount class can make sure the balance is never changed in an invalid way — for example, it can refuse a withdrawal that would make it negative.

Classes and objects

A class is a blueprint. An object is a concrete thing built from that blueprint. One class can produce any number of objects, each with its own data.

Class (blueprint) Objects (instances)
Car your red Toyota, my blue Volvo
BankAccount account #1001, account #1002
Student Alice, Bob, Chen
csharp · try it yourself
using System;

var first = new Dog("Rex", 3);
var second = new Dog("Bella", 7);

first.Bark();
second.Bark();
second.HaveBirthday();
Console.WriteLine(
quot;{second.Name} is now {second.Age}"); class Dog { public string Name; public int Age; public Dog(string name, int age) { Name = name; Age = age; } public void Bark() => Console.WriteLine(
quot;{Name} says: Woof!"); public void HaveBirthday() => Age++; }
Rex says: Woof!
Bella says: Woof!
Bella is now 8

With top-level statements, class declarations go after the statements, as above.

The four pillars of OOP

Pillar Idea C# features Lesson
Encapsulation Hide internal details; expose a safe, simple interface private fields, properties, access modifiers Access Modifiers, Properties
Inheritance Build a new class on top of an existing one and reuse its code class Cat : Animal, base Inheritance
Polymorphism Treat different types through a common interface; each behaves in its own way virtual / override, interfaces Polymorphism
Abstraction Model only what matters; hide complex implementation behind simple contracts abstract classes, interfaces Abstraction, Interfaces

Here is a short preview that touches all four:

csharp · try it yourself
using System;

Shape[] shapes = { new Circle(1), new Square(2) };   // polymorphism

foreach (Shape s in shapes)
    Console.WriteLine(
quot;{s.Name}: area {s.Area():F2}"); abstract class Shape // abstraction { public abstract string Name { get; } public abstract double Area(); } class Circle : Shape // inheritance { private readonly double radius; // encapsulation public Circle(double r) => radius = r; public override string Name => "Circle"; public override double Area() => Math.PI * radius * radius; } class Square : Shape { private readonly double side; public Square(double s) => side = s; public override string Name => "Square"; public override double Area() => side * side; }
Circle: area 3.14
Square: area 4.00

Don't worry if some of that syntax is new — every piece gets its own lesson in this section.

Why OOP?

  • Modelling — classes map directly to concepts in your problem: Order, Invoice, Customer.
  • Reuse — inheritance and composition let you build on existing code.
  • Safety — encapsulation keeps objects in a valid state.
  • Maintainability — changes inside a class do not ripple through the whole program, as long as its public interface stays the same.

Note: C# is not only object-oriented. It also supports functional techniques (lambdas, LINQ, records, pattern matching). Good C# code mixes both styles where each fits best.

Test yourself

What is the relationship between a class and an object?

  1. They are the same thing
  2. A class is a blueprint; an object is an instance of it
  3. An object is a blueprint; a class is an instance
  4. A class is a kind of method

Which OOP pillar is about hiding internal details behind a safe public interface?

  1. Inheritance
  2. Polymorphism
  3. Encapsulation
  4. Recursion