Introduction
In the early days of software development, most applications followed a procedural approach — writing code as a set of instructions that manipulated data. This model quickly became complex, repetitive, and hard to maintain as systems grew.
To solve these challenges, the programming world shifted to the Object-Oriented Programming System (OOPs) — a paradigm that models software around real-world entities like Cars, Employees, or Bank Accounts.
In C#, OOPs allows developers to write modular, reusable, and secure code, making it the foundation for all modern .NET applications — from simple utilities to enterprise systems.
Whether you’re a beginner learning C# or a professional brushing up for interviews, this article will help you understand OOPs in C# from the ground up, with real-world analogies and code examples.
What You’ll Learn in This Article
Traditional Programming vs Object-Oriented Programming System (OOPs)
→ Why we moved away from procedural code and how OOPs improves design.
Core OOPs Concepts in C#
→ Learn what Classes, Objects, Attributes, and Methods are — and how they interact.
Hands-on Examples
→ Build simple yet powerful examples like
CarandEmployeeclasses to apply theory to practice.OOPs Mnemonic (“A PIE”)
→ Remember the four pillars of OOPs — Abstraction, Polymorphism, Inheritance, and Encapsulation — using an easy mnemonic.
Common OOPs Interview Questions
→ Brush up on key questions you’ll face in C# interviews, with concise answers and explanations.
Conclusion
The Traditional (Procedural) Programming Approach
Before the concept of OOPs, most software followed a procedural approach — a step-by-step style where data and functions were written separately.
Let’s take a simple example of a Student Management System built using the procedural approach:
// Procedural Approach
string studentName = "Rahul Sharma";
int studentId = 101;
string course = "Computer Science";
void DisplayStudentDetails()
{
Console.WriteLine($"ID: {studentId}, Name: {studentName}, Course: {course}");
}
DisplayStudentDetails();
Above example works fine but as the project grows:
We end up with scattered variables and functions
The code becomes hard to maintain and debug
Reusability and scalability are limited
Security becomes difficult to enforce (data is globally accessible)
Limitations of Procedural Programming
Scattered Logic and Data
Functions and data existed separately, making it difficult to track where and how data was modified.
As a result, large programs quickly became unmanageable.
Low Reusability
Code written for one part of the system couldn’t be reused easily elsewhere.
Developers often had to rewrite similar functions multiple times.
Poor Maintainability
A small change in one function could unintentionally affect others, leading to bugs and unpredictable behavior.
Security Issues
Global variables were accessible from anywhere in the code, increasing the risk of data corruption and unintended modification.
No Real-World Modeling
Procedural programming doesn’t naturally represent real-world entities like Employees, Students, or Orders.
That’s where the Object-Oriented Programming System (OOPs) comes in — to help you organize data and behavior together into reusable, structured, and secure components.
Object-Oriented Programming System (OOPs)
OOPs is a programming paradigm that structures code around objects — entities that represent real-world things. Each object contains attributes (data) and methods (behavior) that define how it behaves.
OOP helps developers:
Write modular and reusable code
Maintain security and abstraction
Scale projects easily
How OOPs Improves Software Design
The Object-Oriented Programming System (OOPs) was introduced to overcome the limitations of procedural programming by grouping data and behavior together into reusable units called objects .
1. Modularity (Encapsulation)
In OOPs, data and methods are encapsulated within classes, making each class an independent, modular unit. This helps in easier debugging, testing, and maintenance — as each class handles its own logic.
using System;
public class Student
{
private int id;
private string name;
// Public methods to control access
public void SetDetails(int studentId, string studentName)
{
id = studentId;
name = studentName;
}
public void DisplayDetails()
{
Console.WriteLine($"ID: {id}, Name: {name}");
}
}
class Program
{
static void Main()
{
Student s1 = new Student();
s1.SetDetails(101, "Rahul Sharma");
s1.DisplayDetails();
}
}
The Student class manages its own data and behavior — changes inside it won’t affect other parts of the system.
2. Reusability (Inheritance)
Inheritance allows you to reuse existing code instead of writing it again. You can create a base class and extend it into derived classes with additional features.
using System;
// Base class
public class Person
{
public string Name;
public int Age;
}
// Derived class reusing Person
public class Student : Person
{
public string Course;
public void Display()
{
Console.WriteLine($"Name: {Name}, Age: {Age}, Course: {Course}");
}
}
class Program
{
static void Main()
{
Student s1 = new Student();
s1.Name = "Aarav Verma";
s1.Age = 20;
s1.Course = "BCA";
s1.Display();
}
}
Student inherits Name and Age from Person , avoiding duplicate code.
3. Scalability
In OOPs, large applications can be split into multiple interacting classes , allowing different teams to work independently.
public class Student { /* Handles student details */ }
public class Course { /* Handles course data */ }
public class Teacher { /* Handles instructor info */ }
public class Enrollment { /* Connects students and courses */ }
Each class is responsible for one feature. In a real Student Management System, multiple developers can build these modules in parallel without conflicts.
4. Data Security (Encapsulation + Access Modifiers)
OOPs promotes data hiding using access modifiers like private , protected , and public .
This ensures only authorized methods can access or modify internal data.
using System;
public class BankAccount
{
private double balance = 10000;
// Public method to safely modify data
public void Deposit(double amount)
{
if (amount > 0)
balance += amount;
}
public double GetBalance()
{
return balance;
}
}
class Program
{
static void Main()
{
BankAccount acc = new BankAccount();
acc.Deposit(5000);
Console.WriteLine($"Current Balance: {acc.GetBalance()}");
}
}
Balance is private — it can’t be changed directly from outside, protecting data integrity.
5. Real-World Representation
OOPs models real-world entities directly in code. Each class mirrors a real concept — making your design intuitive and natural to understand.
using System;
public class Teacher
{
public string Name;
public string Subject;
public void Teach()
{
Console.WriteLine($"{Name} is teaching {Subject}.");
}
}
class Program
{
static void Main()
{
Teacher t1 = new Teacher();
t1.Name = "Dr. Mehta";
t1.Subject = "Mathematics";
t1.Teach();
}
}
The above program reflects real-world relationships (Teacher → Subject → Teaching behavior).
6. Flexibility and Extensibility (Polymorphism)
Polymorphism allows methods to behave differently based on the object that calls them.
It’s the key to building flexible and extensible systems.
public class Shape
{
public virtual void Draw()
{
Console.WriteLine("Drawing a shape...");
}
}
public class Circle : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing a circle...");
}
}
public class Rectangle : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing a rectangle...");
}
}
class Program
{
static void Main()
{
Shape s1 = new Circle();
Shape s2 = new Rectangle();
s1.Draw(); // Output: Drawing a circle...
s2.Draw(); // Output: Drawing a rectangle...
}
}
The same method ( Draw() ) behaves differently depending on the object — making your code extensible and adaptable to change.
Procedural vs OOPs
| Aspect | Procedural Programming | Object-Oriented Programming System (OOPs) |
|---|---|---|
| Focus | Functions and procedures | Real-world entities (objects) |
| Data Handling | Data and functions are separate | Data and behavior are encapsulated |
| Code Reuse | Limited | High (through inheritance and polymorphism) |
| Security | Low (global data) | High (data hiding and encapsulation) |
| Example Languages | C, Pascal | C# |
Core OOPs Concepts in C#
Classes, Objects, Attributes, and Methods play a unique role in how data and behavior interact.
1. Class — The Blueprint
A class is a blueprint or template that defines what an object will look like and how it will behave. It contains attributes (data) and methods (functions) that describe an entity.
Think of a Class as the HR form that defines what every employee record must have — Name, Salary, and Department.
using System;
public class Employee
{
// Attributes (Data)
public string Name;
public double Salary;
public string Department;
// Method (Behavior)
public void DisplayDetails()
{
Console.WriteLine($"Name: {Name}, Salary: {Salary}, Department: {Department}");
}
}
public class Program
{
public static void Main()
{
Employee emp1 = new Employee();
emp1.Name = "John";
emp1.Salary = 60000;
emp1.Department = "HR";
emp1.DisplayDetails();
}
}
Employee It is a class that defines what information every employee should have and what actions they can perform.
2. Object — The Real Instance
An object is a real-world instance of a class — a concrete example that occupies memory and stores actual data. You can think of the class as a template for employees, and each object as a real employee record created from that template.
Employee emp1 = new Employee();
emp1.Name = "John";
emp1.Salary = 60000;
emp1.Department = "HR";
Employee emp2 = new Employee();
emp2.Name = "Meena";
emp2.Salary = 80000;
emp2.Department = "IT";
Here, emp1 and emp2 are two objects of the Employee class — each holding its own values.
3. Attributes — The Data (State) of an Object
Attributes (also called fields or properties ) represent the data stored in an object.
They define the state of that object at any point in time.
For example:
In
emp1, the attributes might be:Name = "John"Salary = 60000Department = "HR"
Each object can have different attribute values, even though they all share the same structure defined by the class.
4. Methods — The Behavior (Actions) of an Object
Methods are the actions or operations that an object can perform.
They define how the object behaves or interacts with other objects.
In our example, the DisplayDetails() method tells the Employee object how to print its own information.
emp1.DisplayDetails();
emp2.DisplayDetails();
Output
Name: John, Salary: 60000, Department: HRName: Meena, Salary: 80000, Department: IT
Classes, Objects, Attributes, and Methods represent
| Concept | What It Represents | Example in Code |
|---|---|---|
| Class | Blueprint | public class Employee { ... } |
| Object | Real instance | Employee emp1 = new Employee(); |
| Attributes | Data (State) | emp1.Name = "John"; |
| Methods | Behavior (Action) | emp1.DisplayDetails(); |

Join the conversation! Your thoughts help the community grow.