Every customer at a bank has a name, an account number, and a balance — but each customer is different. The bank does not rewrite its entire system for each person. It uses one account template and creates millions of individual accounts from it.
That template is a class. Each real account is an object. Welcome to object-oriented programming (OOP) — the organizing idea behind almost every .NET application you will ever build.
What Is a Class?
A class is a blueprint. It describes:
- Fields / properties — data the object stores (balance, owner name)
- Methods — actions the object can perform (deposit, withdraw)
Think of a Netflix profile template: avatar slot, name field, maturity rating, watchlist. The template is the class. Your sibling's profile and your profile are two objects built from the same template with different values.
What Is an Object?
An object is one concrete instance of a class — a specific thing in memory. You create objects with the new keyword:
BankAccount priyaAccount = new BankAccount();
BankAccount amitAccount = new BankAccount();
Two variables, two objects, same blueprint. Priya's balance can differ from Amit's without affecting each other — like two WhatsApp accounts on different phones using the same app design.
Building a Simple Class
class BankAccount
{
public string OwnerName { get; set; }
public double Balance { get; private set; }
public void Deposit(double amount)
{
if (amount > 0)
Balance = Balance + amount;
}
public bool Withdraw(double amount)
{
if (amount > 0 && Balance >= amount)
{
Balance = Balance - amount;
return true;
}
return false;
}
}
Properties (OwnerName, Balance) expose data with control — notice balance has a private setter so outside code cannot set it directly. Methods encapsulate behaviour and reuse the if-logic you learned in Lesson 4.
Using the Object
BankAccount account = new BankAccount();
account.OwnerName = "Priya Sharma";
account.Deposit(5000);
if (account.Withdraw(1200))
Console.WriteLine("Withdrawal OK. Balance: " + account.Balance);
else
Console.WriteLine("Withdrawal failed.");
You call methods with dot notation: account.Deposit. The object knows its own data — you do not pass balance into every function manually.
Class vs Object Diagram
Class: BankAccount (blueprint)
OwnerName, Balance
Deposit(), Withdraw()
↓ new
Object: Priya's account → Balance 3800
Object: Amit's account → Balance 9200
Real-World Example: Shopping Cart
E-commerce apps model a cart as a class with a list of items and methods like AddItem and CalculateTotal:
class CartItem
{
public string ProductName { get; set; }
public double Price { get; set; }
public int Quantity { get; set; }
}
CartItem phone = new CartItem();
phone.ProductName = "Wireless Earbuds";
phone.Price = 2499;
phone.Quantity = 1;
double lineTotal = phone.Price * phone.Quantity;
Console.WriteLine(phone.ProductName + ": Rs " + lineTotal);
Later, a Cart class might hold many CartItem objects — objects containing objects, like a bag holding multiple labelled boxes.
Common Misconceptions
"Class and object mean the same thing." The class is the cookie cutter; objects are the cookies.
"OOP is only for huge enterprise apps." Even small .NET projects use classes because the language encourages it — and it keeps code readable.
"Everything should be public." Hiding internal details (private) prevents other code from breaking invariants — like preventing negative balances.
"One class per file is mandatory." It is a strong convention in C#, not a compiler rule. Follow it for clarity.
Quick Recap
- A class is a blueprint; an object is a living instance.
- Properties hold data; methods define behaviour.
new ClassName()creates an object.- Dot syntax accesses members:
account.Deposit(100). - OOP groups related variables and logic — less spaghetti code.
Summary
Classes turn scattered variables into meaningful bundles — a bank account, a user profile, an order. That structure mirrors how humans think about the world, which is why OOP dominates .NET development.
In Lesson 6 you will combine everything — variables, if-else, classes — into your first complete console application from scratch.
Frequently Asked Questions
Deposit or Withdraw on a bank account.new keyword allocates memory and creates a fresh object from the class blueprint.Key Takeaways
- Classes are blueprints; objects are instances created with
new. - Properties store state; methods define behaviour.
- OOP mirrors real-world entities: accounts, carts, profiles.
- Encapsulation (private setters) protects data integrity.
- Dot notation connects objects to their members.