3. Object-Oriented Programming
The Four Pillars of OOP
┌─────────────────────────────────────────────────────────────────────────────┐
│ OBJECT-ORIENTED PROGRAMMING │
│ │
│ ┌────────────────┐ ┌────────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ ENCAPSULATION │ │ ABSTRACTION │ │ INHERITANCE │ │ POLYMORPHISM │ │
│ │────────────────│ │────────────────│ │──────────────│ │──────────────│ │
│ │ Hide details │ │ Hide complex │ │ Reuse code │ │ One interface│ │
│ │ Use properties │ │ Show essential │ │ Extend types│ │ Many forms │ │
│ │ Access control │ │ Interfaces │ │ Base/derived│ │ Overriding │ │
│ └────────────────┘ └────────────────┘ └──────────────┘ └──────────────┘ │
└─────────────────────────────────────────────────────────────────────────────┘
1. Classes and Objects
Class Definition Anatomy
Object Creation (new keyword)
Reference Semantics
this Keyword
Object Initializers (C# 3.0+)
2. Structs
Struct vs Class
Feature
Struct
Class
Struct Declaration
Struct Limitations
readonly struct (C# 7.2+)
ref struct (C# 7.2+)
When to Use Struct vs Class
3. Constructors
Default Constructor
Parameterized Constructor
Constructor Overloading
Constructor Chaining (this)
Copy Constructor Pattern
Static Constructor
Private Constructor (Singleton Pattern)
Primary Constructors (C# 12.0+)
4. Destructors (Finalizers)
Syntax
When Destructors Run
Destructor vs Dispose
Feature
Destructor (~)
Dispose()
Why to Avoid Destructors
5. Properties
Full Property Syntax
Auto-Implemented Properties (C# 3.0+)
Property Access Levels
Read-Only Properties
Computed Properties
Init-Only Setters (C# 9.0+)
required Properties (C# 11.0+)
Property vs Field Comparison
Feature
Field
Property
Expression-Bodied Properties (C# 6.0+)
6. Access Modifiers
Access Levels
Modifier
Accessible From
Visual Accessibility Diagram
Examples
Default Access Levels
7. Methods
Method Signature
Method Overloading
Method Parameters
Value Parameters (Default)
ref Parameters
out Parameters
in Parameters (C# 7.2+)
params Parameters
Optional Parameters (C# 4.0+)
Named Arguments (C# 4.0+)
Expression-Bodied Methods (C# 6.0+)
Local Functions (C# 7.0+)
Static Local Functions (C# 8.0+)
8. The Four Pillars of OOP
Pillar 1: Encapsulation
Pillar 2: Abstraction
Abstract Classes
Interfaces
Multiple Interface Implementation
Explicit Interface Implementation
Default Interface Methods (C# 8.0+)
Static Abstract Members (C# 11.0+)
Abstract Class vs Interface
Feature
Abstract Class
Interface
Pillar 3: Inheritance
Single Inheritance
Multiple Inheritance (Interfaces)
base Keyword
Calling Base Constructors
sealed Classes (Prevent Inheritance)
Inheritance Hierarchy Example
Pillar 4: Polymorphism
Compile-Time Polymorphism (Method Overloading)
Runtime Polymorphism (Method Overriding)
virtual, override, sealed
Method Hiding with new
Keyword
Purpose
Polymorphic?
9. Static Members
Static Fields
Static Methods
Static Constructors
Static Classes
Feature
Instance
Static
10. Partial Classes (C# 2.0+)
Split Class Definition
Use Cases
Partial Methods (C# 3.0+)
Partial Properties (C# 13.0+)
11. Extension Methods (C# 3.0+)
Syntax
Rules and Limitations
Common Use Cases
12. Nested Classes
Inner Classes
Access to Outer Class Members
When to Use Nested Classes
13. Anonymous Types (C# 3.0+)
Syntax
Use with LINQ
Limitations
14. Records (C# 9.0+)
Record Classes (Reference Types)
Positional Records
with Expressions (Non-Destructive Mutation)
Value-Based Equality
Record Structs (C# 10.0+)
Record vs Class vs Struct
Feature
Class
Struct
Record (class)
Record struct
When to Use Records
15. Operator Overloading
Overloadable Operators
Syntax
Comparison Operators (Must Override in Pairs)
Best Practices
16. Conversion Operators
implicit operator (Automatic Conversion)
explicit operator (Manual Conversion)
When to Use Each
17. Indexers
Syntax
Multiple Indexers
Overloading Indexers
Read-Only Indexer
Common Pitfalls
1. Forgetting virtual/override
2. Using new Instead of override
3. Not Calling base Constructor
4. Struct Mutability Issues
5. Property vs Field in Interfaces
6. Forgetting to Override Equals and GetHashCode
Best Practices
1. Encapsulation
2. Use Auto-Properties When Possible
3. Prefer Composition Over Inheritance
4. Use Interfaces for Contracts
5. Mark Classes sealed When Not Designed for Inheritance
6. Use Records for Immutable Data
7. Initialize Collections in Constructor or Inline
8. Use Expression-Bodied Members for Simple Properties
9. Validate in Constructors
10. Use readonly for Immutable Fields
Quick Reference: Decision Trees
Should I Use Class, Struct, or Record?
Should I Use Abstract Class or Interface?
Should Method Be virtual?
Last updated