Roadmap › 1. .NET › Step 10
OOP
OOP Concepts in C#
Classes and objects, the four pillars (encapsulation, abstraction, inheritance, polymorphism), interfaces vs abstract classes, composition and object relationships.
What it is & where it is used
Object-Oriented Programming (OOP) organises code around objects — bundles of data (state) and behaviour (methods) — instead of loose functions and global data. C# is designed around OOP, and every .NET interview tests it.
The four pillars:
| Pillar | One line | C# features |
|---|---|---|
| Encapsulation | Hide internal state; expose a controlled API | private fields, properties, access modifiers |
| Abstraction | Show what an object does, hide how | interface, abstract class |
| Inheritance | Reuse and extend a base type ("is-a") | : BaseClass, virtual/override, sealed |
| Polymorphism | One interface, many implementations | Overloading (compile time), overriding (runtime) |
Versions & history
OOP is a paradigm, not a versioned product, but C# has added OOP-related features over time:
| C# | OOP-related additions |
|---|---|
| 1.0 | Classes, interfaces, inheritance, virtual/override, abstract, sealed, properties, events |
| 2.0 | Generics, static classes, partial classes |
| 3.0 | Auto-properties, object initialisers, extension methods |
| 8.0 | Default interface methods (interfaces can contain implementations) |
| 9.0 | Records (value equality), init accessors, covariant return types |
| 11 | required members, static abstract members in interfaces |
| 12 | Primary constructors for classes and structs |
| 14 | Extension members (extension properties) |
History: Simula 67 introduced classes; Smalltalk (1970s) popularised "everything is an object"; C++, Java and C# made OOP mainstream.
Lifecycle
Object lifecycle in .NET
- Type loading — the first time a type is used, the CLR loads it and runs its static constructor once.
- Allocation —
newreserves memory on the managed heap (value types: inline). - Construction — field initialisers run, then base-class constructor, then the derived constructor body (
: base(...)/: this(...)chaining). - Use — methods called, state changes through its public API.
- Unreachable — no references remain.
- Collection — GC reclaims memory; a finaliser runs first if defined.
- Disposal (optional, deterministic) —
Dispose()viausingreleases unmanaged resources earlier.
OOP design lifecycle (OOAD): requirements → identify nouns (classes) and verbs (methods) → define responsibilities and relationships (UML class diagram) → apply principles (SOLID) and patterns → implement → unit test → refactor.
Tools used to develop
- Visual Studio Class Designer / Class Diagram — visualise classes and inheritance.
- UML tools — draw.io, PlantUML, Mermaid, StarUML, Lucidchart.
- ReSharper / Rider — refactorings: extract interface, pull members up, introduce parameter object.
- SharpLab.io — see what the compiler generates (records, primary constructors).
- xUnit + Moq/NSubstitute — test classes through interfaces.