C# Polymorphism

C# Polymorphism

Polymorphism in C# is an OOP concept that allows objects to take many forms.
It enables a method, class, or object to behave differently based on context.

There are two types of polymorphism in C#:

  1. Compile-time Polymorphism (Method Overloading / Operator Overloading)

  2. Run-time Polymorphism (Method Overriding)


🔹 1. Compile-time Polymorphism

Method Overloading

Same method name, different parameters:


 


Operator Overloading

C# allows custom behavior for operators:


 


🔹 2. Run-time Polymorphism

Method Overriding

  • Base class method is marked virtual

  • Derived class method uses override


 


Using base Keyword

Call base class method from derived class:



 


🔹 Advantages of Polymorphism

Code reusability
Flexibility and scalability
Simplifies maintenance
Supports dynamic behavior


🔹 Common Mistakes

❌ Forgetting virtual in base class
❌ Using same signature incorrectly
❌ Confusing overloading (compile-time) with overriding (run-time)


🔹 Summary

  • Polymorphism = “many forms”

  • Compile-time → method/operator overloading

  • Run-time → method overriding using virtual & override

  • Supports flexible and reusable code

You may also like...