Complete C# Tutorial

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 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! πŸš€

Leave a Comment

Share this Doc

Polymorphism in C#

Or copy link