Section 1: OOP Fundamentals | 7 Important Questions | C# / .NET
1. 🔴 What is Object-Oriented Programming (OOP), and what are its four main pillars?
What is OOP?
Object-Oriented Programming (OOP) is a programming paradigm that organizes software around classes and objects, combining data (properties/fields) and behavior (methods).
OOP helps developers build applications that are modular, reusable, maintainable, and extensible.
Four main pillars of OOP
1. Encapsulation
Protect data and control access to it.
2. Abstraction
Expose essential functionality and hide implementation details.
3. Inheritance
Derive a new class from an existing class.
4. Polymorphism
Use one interface or method contract with different implementations.
C# example demonstrating all four pillars
// Abstraction
abstract class Employee
{
// Encapsulation
private decimal salary;
public decimal Salary
{
get => salary;
set
{
if (value < 0)
throw new ArgumentException("Invalid salary");
salary = value;
}
}
// Polymorphism
public abstract decimal CalculateBonus();
}
// Inheritance
class Manager : Employee
{
public override decimal CalculateBonus()
{
return Salary * 0.20m;
}
}
class Developer : Employee
{
public override decimal CalculateBonus()
{
return Salary * 0.10m;
}
}
Usage:
Employee emp1 = new Manager { Salary = 100000 };
Employee emp2 = new Developer { Salary = 100000 };
Console.WriteLine(emp1.CalculateBonus()); // 20000
Console.WriteLine(emp2.CalculateBonus()); // 10000
Code explanation
-
Encapsulation:
salaryis private, and the property validates modifications. -
Abstraction:
Employeedefines a bonus calculation contract without implementing it. -
Inheritance:
ManagerandDeveloperinherit fromEmployee. -
Polymorphism:
CalculateBonus()executes different implementations depending on the actual object.
Real-time scenario
In a banking application, Account can be an abstract base class. SavingsAccount and CurrentAccount inherit common functionality while implementing different interest calculation rules.
Interview answer
OOP is a programming approach based on classes and objects. Its four pillars are Encapsulation, Abstraction, Inheritance, and Polymorphism. These principles help build reusable, secure, flexible, and maintainable applications.
2. 🔴 What is the difference between a class and an object in C#?
What is a class?
A class is a blueprint or template that defines the properties, fields, and methods of an object.
What is an object?
An object is an instance of a class created at runtime. It contains its own instance data and can execute methods defined by its class.
C# example
// Class definition
public class Employee
{
public int Id { get; set; }
public string Name { get; set; } = "";
public void Display()
{
Console.WriteLine($"{Id} - {Name}");
}
}
// Object creation
Employee emp1 = new Employee();
emp1.Id = 101;
emp1.Name = "John";
Employee emp2 = new Employee();
emp2.Id = 102;
emp2.Name = "David";
emp1.Display();
emp2.Display();
Output:
101 - John
102 - David
Differences
|
Class |
Object |
|---|---|
|
Blueprint or definition |
Instance of a class |
|
Defines properties and methods |
Holds actual instance values |
|
Does not require instance creation to be defined |
Usually created using |
|
One class can define many objects |
Each object has its own instance state |
|
Example: |
Example: |
Real-time scenario
In an employee management system, Employee is a class, while individual employees such as John and David are represented by separate objects.
Interview answer
A class is a blueprint that defines the structure and behavior of objects. An object is an instance of a class created at runtime. Multiple objects can be created from the same class, each maintaining its own instance data.
3. 🔴 What are the advantages of OOP over procedural programming?
Explanation
Procedural programming organizes code around functions and procedures, whereas OOP organizes code around objects that combine data and behavior.
OOP is particularly useful for large applications involving multiple modules, business entities, and development teams.
Advantages of OOP
|
Advantage |
Explanation |
|---|---|
|
Code reusability |
Reuse functionality through inheritance and composition |
|
Encapsulation |
Protect internal data through controlled access |
|
Maintainability |
Organize related logic into separate classes |
|
Extensibility |
Add new functionality with minimal changes to existing code |
|
Polymorphism |
Support multiple implementations through common contracts |
|
Testability |
Test components independently using interfaces and dependency injection |
|
Modularity |
Divide applications into smaller, manageable components |
C# example
Procedural-style approach:
decimal CalculateSavingsInterest(decimal amount)
{
return amount * 0.04m;
}
decimal CalculateFixedDepositInterest(decimal amount)
{
return amount * 0.07m;
}
OOP approach:
public interface IInterestCalculator
{
decimal Calculate(decimal amount);
}
public class SavingsInterest : IInterestCalculator
{
public decimal Calculate(decimal amount)
{
return amount * 0.04m;
}
}
public class FixedDepositInterest : IInterestCalculator
{
public decimal Calculate(decimal amount)
{
return amount * 0.07m;
}
}
Usage:
IInterestCalculator calculator = new SavingsInterest();
Console.WriteLine(calculator.Calculate(10000)); // 400
calculator = new FixedDepositInterest();
Console.WriteLine(calculator.Calculate(10000)); // 700
Why is OOP better in this scenario?
The application can support additional interest calculation methods without modifying existing calculator classes.
For example, adding RecurringDepositInterest requires implementing the same interface.
This supports the Open/Closed Principle of SOLID.
Real-time scenario
In ASP.NET Core Web API, Controllers, Services, and Repositories are commonly organized using classes and interfaces.
EmployeeController
|
v
IEmployeeService
|
v
EmployeeService
|
v
IEmployeeRepository
|
v
EmployeeRepository
|
v
SQL Server
This separation makes application logic easier to test, maintain, and extend.
Interview answer
OOP provides code reusability, encapsulation, modularity, maintainability, and extensibility. Compared with procedural programming, OOP is often more suitable for large enterprise applications because it organizes related data and behavior into independent components.
Important: Procedural programming is not inherently inferior. It can be simpler and more appropriate for small, sequential tasks.
4. 🔴 What is the difference between a class, struct, and record in C#?
Explanation
C# provides class, struct, and record for defining custom data types.
The primary differences involve value/reference semantics, equality, inheritance, and intended usage.
Class
A class is a reference type commonly used for objects with identity and mutable state.
public class Employee
{
public int Id { get; set; }
public string Name { get; set; } = "";
}
Struct
A struct is a value type generally used for small data structures that represent a single value.
public struct Point
{
public int X { get; set; }
public int Y { get; set; }
}
Record
A record is designed primarily for data-centric models and provides built-in value-based equality.
A record is a reference type by default.
public record EmployeeDto(int Id, string Name);
A value-type record can also be defined:
public readonly record struct PointDto(int X, int Y);
Key differences
|
Feature |
Class |
Struct |
Record class |
|---|---|---|---|
|
Type |
Reference |
Value |
Reference |
|
Default equality |
Reference identity |
Value-based |
Value-based |
|
Inheritance |
Supported |
No struct inheritance |
Supported between record classes |
|
Mutability |
Supported |
Supported |
Supported |
|
Best use |
Business entities |
Small values |
DTOs, data models |
|
Can be |
Yes |
Only |
Yes |
Note: Records are not always immutable. Positional record properties are init-only by default, but mutable record properties can be declared.
C# example: Equality comparison
public class EmployeeClass
{
public int Id { get; set; }
}
public record EmployeeRecord(int Id);
public struct EmployeeStruct
{
public int Id { get; set; }
}
Usage:
var c1 = new EmployeeClass { Id = 101 };
var c2 = new EmployeeClass { Id = 101 };
Console.WriteLine(c1 == c2); // False
var r1 = new EmployeeRecord(101);
var r2 = new EmployeeRecord(101);
Console.WriteLine(r1 == r2); // True
var s1 = new EmployeeStruct { Id = 101 };
var s2 = new EmployeeStruct { Id = 101 };
Console.WriteLine(s1.Equals(s2)); // True
Why different results?
-
Class: Default
==compares object references. -
Record: Generated equality compares corresponding values.
-
Struct: Default
Equals()compares values. Ordinary structs do not have a generated==operator.
Real-time scenario
In ASP.NET Core:
-
Class:
EmployeeService,Employee,Order. -
Struct:
DateTime,Guid, custom coordinates. -
Record:
EmployeeDto,CreateOrderRequest,ApiResponse.
Interview answer
A class is a reference type, while a struct is a value type. A record is designed for data-centric models with built-in value-based equality. Records can be reference types (record class) or value types (record struct). Classes are commonly used for entities and services, structs for small values, and records for DTOs.
5. 🔴 What is the difference between value types and reference types in C#?
Explanation
C# types are broadly classified into value types and reference types.
-
Value type: A variable directly contains its value.
-
Reference type: A variable holds a reference to an object.
Value type example
int a = 10;
int b = a;
b = 20;
Console.WriteLine(a); // 10
Console.WriteLine(b); // 20
Explanation: When assigning a to b, the value is copied. Changing b does not affect a.
Reference type example
public class Employee
{
public string Name { get; set; } = "";
}
Employee emp1 = new Employee { Name = "John" };
Employee emp2 = emp1;
emp2.Name = "David";
Console.WriteLine(emp1.Name); // David
Console.WriteLine(emp2.Name); // David
Explanation: Both variables refer to the same object. Changing its property through emp2 is visible through emp1.
Conceptual memory representation
Value type:
a = 10
|
| Copy value
v
b = 10
Change b = 20
a = 10
b = 20
Reference type:
emp1 ----+
|
+----> Employee Object
| Name = "John"
emp2 ----+
Change emp2.Name = "David"
Both references see Name = "David"
Differences
|
Value type |
Reference type |
|---|---|
|
Contains actual value |
Contains reference to object |
|
Assignment copies value |
Assignment copies reference |
|
Variables normally have independent values |
Multiple references can point to same object |
|
Examples: |
Examples: |
|
Can be nullable using |
Supports null references |
Important interview point: Stack vs Heap
A common misconception is:
Value types are always stored on the stack, and reference types are always stored on the heap.
This is not always true.
Value types may be stored inside heap-allocated objects, local variables, or other storage locations. Reference-type objects are generally heap-allocated, but their reference variables can be stored in different locations.
The fundamental distinction is value semantics versus reference semantics, not simply stack versus heap.
Interview answer
Value types store their data directly, and assignments normally copy the value. Reference types store references to objects, so multiple variables can refer to the same object. Examples of value types include int and struct, while reference types include class, string, and arrays.
6. 🟡 What is the difference between an object, reference, and instance?
Explanation
These three terms are closely related but have different meanings.
|
Term |
Meaning |
|---|---|
|
Object |
An entity created at runtime with data and behavior |
|
Instance |
A particular object created from a class |
|
Reference |
A value used to access an object |
Object and instance are often used interchangeably when discussing classes.
C# example
public class Employee
{
public string Name { get; set; } = "";
}
Employee emp1 = new Employee { Name = "John" };
Employee emp2 = emp1;
Console.WriteLine(emp1.Name); // John
Console.WriteLine(emp2.Name); // John
Explanation
Class: Employee
|
| new Employee()
v
Object / Instance
Name = "John"
^
|
+---+---+
| |
emp1 emp2
-
Employeeis the class. -
new Employee()creates an instance. -
The created
Employeeis an object. -
emp1andemp2are reference variables. -
Both references point to the same object.
Additional example
Employee emp1 = new Employee();
Employee emp2 = new Employee();
Console.WriteLine(
ReferenceEquals(emp1, emp2)); // False
Here, two separate objects are created.
Compare this with:
Employee emp1 = new Employee();
Employee emp2 = emp1;
Console.WriteLine(
ReferenceEquals(emp1, emp2)); // True
Here, both references point to the same object.
Interview answer
An object is an entity created at runtime, and an instance is a particular object of a class. A reference is a value used to access an object. Multiple references can point to the same object, but creating objects separately produces distinct instances.
7. 🟡 What are the different types of classes available in C#?
C# supports several class forms and modifiers, each serving a different purpose.
1. Concrete class
A concrete class can be instantiated directly.
public class Employee
{
public void Display()
{
Console.WriteLine("Employee details");
}
}
Employee emp = new Employee();
emp.Display();
Used for: Normal business entities, services, and other instantiable objects.
2. Abstract class
An abstract class cannot be instantiated directly. It can contain both abstract and implemented members.
public abstract class Animal
{
public abstract void Sound();
public void Sleep()
{
Console.WriteLine("Sleeping...");
}
}
public class Dog : Animal
{
public override void Sound()
{
Console.WriteLine("Bark");
}
}
Used for: Defining common behavior and requiring derived classes to implement specific functionality.
3. Sealed class
A sealed class prevents other classes from inheriting from it.
public sealed class SecurityHelper
{
public void Validate()
{
Console.WriteLine("Validated");
}
}
The following is invalid:
// Compilation error
public class MyHelper : SecurityHelper
{
}
Used for: Preventing inheritance when a class is not designed to be extended.
4. Static class
A static class cannot be instantiated and contains only static members.
public static class MathHelper
{
public static int Add(int a, int b)
{
return a + b;
}
}
Usage:
int result = MathHelper.Add(10, 20);
Console.WriteLine(result); // 30
Used for: Stateless utility methods, helper functions, and extension-method containers.
5. Partial class
A partial class allows a single class definition to be split across multiple files.
Employee.cs
public partial class Employee
{
public int Id { get; set; }
}
Employee.Details.cs
public partial class Employee
{
public void Display()
{
Console.WriteLine(Id);
}
}
Usage:
Employee emp = new Employee { Id = 101 };
emp.Display(); // 101
Used for: Source-generated code, Windows Forms designer code, and large class definitions that need to be split.
6. Nested class
A nested class is declared inside another class.
public class Department
{
public class Employee
{
public string Name { get; set; } = "";
}
}
Usage:
Department.Employee emp = new();
emp.Name = "John";
Used for: Helper types that are closely associated with their containing class.
7. Generic class
A generic class works with different data types while maintaining type safety.
public class Repository<T>
{
public void Save(T entity)
{
Console.WriteLine("Entity saved");
}
}
Usage:
Repository<Employee> empRepo = new();
Repository<string> textRepo = new();
empRepo.Save(new Employee());
textRepo.Save("Sample");
Used for: Generic repositories, collections, reusable services, and data-processing components.
Summary comparison
|
Class type |
Main purpose |
|---|---|
|
Concrete |
Create normal objects |
|
Abstract |
Define common behavior and contracts |
|
Sealed |
Prevent inheritance |
|
Static |
Group static utility members |
|
Partial |
Split class definition across files |
|
Nested |
Define a class inside another class |
|
Generic |
Support multiple data types |
Additional interview point: These categories are not mutually exclusive. For example, a generic class can also be abstract or partial.
Interview answer
C# supports concrete, abstract, sealed, static, partial, nested, and generic classes. Concrete classes can be instantiated, abstract classes provide base functionality, sealed classes prevent inheritance, static classes provide utility methods, partial classes split definitions, nested classes organize related types, and generic classes support reusable type-safe implementations.
Quick Interview Revision – All 7 Questions
|
Question |
Key points to remember |
|---|---|
|
1. OOP and pillars |
Encapsulation, Abstraction, Inheritance, Polymorphism |
|
2. Class vs Object |
Blueprint vs instance |
|
3. OOP advantages |
Reusability, maintainability, modularity, extensibility |
|
4. Class vs Struct vs Record |
Reference type, value type, value-based equality |
|
5. Value vs Reference |
Copy value vs copy reference |
|
6. Object vs Reference vs Instance |
Entity, access reference, particular object |
|
7. Types of classes |
Concrete, Abstract, Sealed, Static, Partial, Nested, Generic |
Interview priority: Questions 1, 4, and 5 deserve particular attention because they lead naturally to follow-up questions about abstraction, equality, memory allocation, boxing/unboxing, and inheritance.