Polymorphism in C# β Simple Guide with Examples
π Introduction
Hey buddy! π Have you ever wondered how the same function can behave differently in different situations? π€
Imagine you have a payment system where users can pay using:
π΅ Cash
π³ Credit Card
π± UPI (like Google Pay, PhonePe etc.)
Each payment processes differently, but the method name remains the same (MakePayment
). The same action works differently! That’s exactly what Polymorphism in C# does in programming.
In simple words, polymorphism allows a method to have different behaviors based on the context. This makes our code flexible, reusable, and super easy to maintain.
Letβs break it down step by step, with fun examples and hands-on coding! π
π What You Are Going to Learn in This Lesson
βοΈ What is Polymorphism in C#?
βοΈ Why is polymorphism important?
βοΈ Types of polymorphism (Compile-time & Runtime)
βοΈ Real-world examples of polymorphism
βοΈ Complete programs with output
π What is Polymorphism in C#?
The word Polymorphism means “many forms”.
In C#, polymorphism allows you to use the same method name for different behaviors. It helps in code reusability and scalability.
Polymorphism in C# is divided into two types:
1οΈβ£ Compile-time Polymorphism (Method Overloading)
2οΈβ£ Runtime Polymorphism (Method Overriding)
Simple Example of Polymorphism
using System;
namespace Function_Overloading
{
class Program
{
static void Main()
{
Calculator.Sum(); // Calls the method with no parameters
Calculator.Sum(5, 4); // Calls the method with int parameters
Calculator.Sum(9.3f, 8.6f); // Calls the method with float parameters
Calculator.Sum("Hello"); // Calls the method with a string parameter
Console.ReadLine();
}
}
static class Calculator
{
public static void Sum() // Method 1: No parameters
{
Console.WriteLine("No Value Provided");
}
public static void Sum(int x, int y) // Method 2: Two int parameters
{
Console.WriteLine($"Sum of {x} and {y} is {x + y}");
}
public static void Sum(float x, float y) // Method 3: Two float parameters
{
Console.WriteLine($"Sum of {x} and {y} is {x + y}");
}
public static void Sum(string s) // Method 4: One string parameter
{
Console.WriteLine($"{s} - is not a numeric value");
}
}
π― Output
No Value Provided
Sum of 5 and 4 is 9
Sum of 9.3 and 8.6 is 17.9
Hello - is not a numeric value
π§ What is Happening in This Code?
In the Calculator
class, we have four versions of the method sum()
, each with different parameters:
1οΈβ£ sum()
β No parameters (prints “No Value Provided”).
2οΈβ£ sum(int, int)
β Takes two integers and returns their sum.
3οΈβ£ sum(float, float)
β Takes two floats and returns their sum.
4οΈβ£ sum(string)
β Takes a string and prints that it’s not a numeric value.
This is Method Overloading, which is an example of Compile-time Polymorphism.
π‘ Why is This Important?
- Improves Code Readability β Instead of using different function names (
SumInt()
,SumFloat()
, etc.), we use one method name (Sum()
) and change only parameters. - Enhances Code Reusability β We reuse the same function name for different types of calculations.
- Reduces Complexity β No need for multiple function names, making code cleaner and more maintainable.
π€ What if This Was Not Polymorphism?
Without polymorphism, we would need to write different method names like:
public static void SumInt(int x, int y) { }
public static void SumFloat(float x, float y) { }
public static void SumString(string s) { }
This makes the code harder to manage and less readable.
Instead, Polymorphism allows us to use the same method name (Sum()
) with different parameter lists.
π― Conclusion
In this lesson, we learned:
βοΈ What Polymorphism in C# is
βοΈ Types of Polymorphism in C#.
βοΈ Why polymorphism is important?
βοΈ Hands-on coding examples to understand it easily
Polymorphism makes your code powerful, efficient, and reusable! π
βοΈ Next What?
Awesome job! π Now that you understand Polymorphism in C#, it’s time to dive deeper into Compile-time Polymorphism (Method Overloading in C#).
So, grab your coffee β and letβs continue learning! π