Classes and objects are the foundation of Object-Oriented Programming (OOP).
If you understand classes and objects properly, the rest of OOP becomes easy.
In this lesson, you will learn:
- What is a class?
- What is an object?
- How to create classes and objects in C#
- Properties, fields, and methods
- Real-world examples
- Practical C# examples
🌟 What is a Class in C#?
A class is a blueprint or template for creating objects.
✔ Simple Definition:
A class defines how an object will look and behave.
It contains:
- Fields (variables)
- Properties (get/set values)
- Methods (actions)
- Constructors
- Events (advanced)
🟩 Real-World Analogy of a Class
✔ Class = Car Blueprint
The blueprint defines:
- Color
- Model
- Speed
- Features
But you cannot drive the blueprint.
You can only drive a real car, which is the object.
🌟 What is an Object in C#?
An object is an instance of a class.
✔ Simple Definition:
Object = something created from a class.
If class = blueprint,
then object = real product.
🧑💻 Basic Class & Object Example in C#
✔ Step 1: Create a Class
public class Car
{
public string Brand;
public string Color;
public void Start()
{
Console.WriteLine($"{Brand} is starting...");
}
}
✔ Step 2: Create an Object from Class
Car myCar = new Car();
myCar.Brand = "Honda";
myCar.Color = "Red";
myCar.Start();
Output:
Honda is starting...
✔ Car is the class
✔ myCar is the object
🧱 Components of a Class
A class usually contains:
1️⃣ Fields (Variables inside class)
public string name;
public int age;
2️⃣ Properties (Encapsulated fields)
public string Name { get; set; }
public int Age { get; set; }
3️⃣ Methods (Actions or behaviors)
public void Speak()
{
Console.WriteLine("Hello!");
}
4️⃣ Constructors (Special method to initialize objects)
public Person(string name)
{
Name = name;
}
🔍 Detailed Example — Student Class
✔ Class Definition
public class Student
{
public string Name;
public int Age;
public void Study()
{
Console.WriteLine($"{Name} is studying.");
}
}
✔ Object Creation
Student s = new Student();
s.Name = "Amit";
s.Age = 20;
s.Study();
Output:
Amit is studying.
🌈 Multiple Objects from One Class
Car car1 = new Car();
car1.Brand = "BMW";
Car car2 = new Car();
car2.Brand = "Audi";
car1.Start(); // BMW is starting...
car2.Start(); // Audi is starting...
✔ Both objects share the same blueprint
✔ But hold different data
🧠 Why Do We Use Classes & Objects?
✔ Reusability
Create once → use many times.
✔ Organization
Separate data and behavior properly.
✔ Modularity
Divide large applications into smaller classes.
✔ Real-world modeling
Makes programming intuitive.
✔ Code maintenance
Change in one place affects all instances.
🎯 Real-World Examples of Classes
| Real-World Object | OOP Class |
|---|---|
| Student | Student |
| Employee | Employee |
| Book | Book |
| Bank Account | BankAccount |
| Car | Car |
| Mobile | Mobile |
🧩 Practical Example — Product Class
✔ Class:
public class Product
{
public string Name;
public double Price;
public void Display()
{
Console.WriteLine($"Product: {Name}, Price: {Price}");
}
}
✔ Objects:
Product p1 = new Product();
p1.Name = "Laptop";
p1.Price = 55000;
Product p2 = new Product();
p2.Name = "Mouse";
p2.Price = 500;
p1.Display();
p2.Display();
🔎 Classes vs Objects (Easy Table)
| Classes | Objects |
|---|---|
| Blueprint | Instance |
| Defines how things look | Represents actual thing |
| Does not hold data | Holds data |
| Example: Car class | Your Honda car |
| Example: Student class | Student named Riya |
📝 Mini Exercise
Create a class named Mobile with:
Properties:
- Brand
- Model
- Price
Method:
- Details() → prints all details
Then create two objects with different values.
🔍 FAQs
Q1: Can a class exist without an object?
Yes, but you cannot use it until you create an object.
Q2: How many objects can be created from one class?
Unlimited.
Q3: Can a class have multiple constructors?
Yes, constructors can be overloaded.
Q4: Is a class a reference type?
Yes, classes are reference types in C#.
🎉 Conclusion
Classes and objects form the building blocks of C# and OOP.
A class defines the structure, and objects bring it to life.
You now understand:
✔ What is a class
✔ What is an object
✔ How to create classes
✔ How to create objects
✔ Real-world analogies
✔ Why classes & objects matter