Object-oriented programming in C#
Explore encapsulation, abstraction, inheritance, and polymorphism through small C# examples, design checks, and a quick quiz.
Explore the concepts
Pick a concept. Read the code. Make the call.
Each example contrasts a common design problem with one possible improvement. The best design still depends on context.
Protect state; expose meaningful operations.
Encapsulation
1Private state
2Validated method
3Safe object
What to notice
Public mutable fields let any caller break the account balance invariant.
C# / HTTP / SQL sample
A design to question
public class BankAccount
{
public decimal Balance;
}
account.Balance = -5000;Right or wrong: is this version a good fit?
Quick knowledge check
Put your interview hat on
Which change best demonstrates encapsulation?
Based on our OOP interview guide. Examples are simplified for learning; adapt patterns to the requirements of your application.