Complete C# Tutorial

Compile-time Polymorphism (Method Overloading) in C# with Examples

πŸ‘‹ Hey, C# Learner!

Have you ever wondered how the same method name can do different tasks? πŸ€”
For example, a calculator adds numbers, but it should also work for decimals, three numbers, or even strings!

This is where Compile-time Polymorphism in C# (also called Method Overloading) comes into play! πŸš€

🧐 What is Compile-time Polymorphism in C#?

Compile-time Polymorphism (also called Method Overloading) means:

βœ” The same method name but with different parameters.
βœ” The compiler decides which method to call at compile-time.
βœ” No need for inheritance!

It helps make code clean, readable, and reusable! 🎯

πŸ“ Syntax of Method Overloading in C#

				
					class ClassName
{
    public void MethodName(int a) { }
    public void MethodName(double a) { }
    public void MethodName(int a, int b) { }
}
				
			

πŸš€ Same method name, different parameters!

πŸ’» Simple Example of Method Overloading in C#

Let’s start with a super easy example!

				
					using System;

class MathOperations
{
    public void Add(int a, int b)
    {
        Console.WriteLine($"Sum of integers: {a + b}");
    }

    public void Add(double a, double b)
    {
        Console.WriteLine($"Sum of doubles: {a + b}");
    }

    public void Add(string a, string b)
    {
        Console.WriteLine($"Concatenated String: {a + b}");
    }
}

class Program
{
    static void Main()
    {
        MathOperations math = new MathOperations();

        math.Add(5, 10);         // Calls int version
        math.Add(2.5, 3.5);      // Calls double version
        math.Add("Hello, ", "World!"); // Calls string version
    }
}
				
			
🎯 Output:
				
					Sum of integers: 15  
Sum of doubles: 6  
Concatenated String: Hello, World!  
				
			

🧐 Explanation:

1️⃣ Same method name (Add) but different parameters.
2️⃣ The compiler chooses the correct method based on arguments.
3️⃣ The int version adds integers.
4️⃣ The double version adds decimals.
5️⃣ The string version joins words!

πŸ’‘ This is Compile-time Polymorphism in action! πŸš€

🌎 Real-World Example of Method Overloading in C#

Think about a restaurant ordering system.
Customers can order:

βœ” Just a drink
βœ” A meal with a drink
βœ” A meal with dessert and drink

Let’s implement it!

				
					using System;

class Restaurant
{
    public void Order(string drink)
    {
        Console.WriteLine($"Order placed: {drink}");
    }

    public void Order(string meal, string drink)
    {
        Console.WriteLine($"Order placed: {meal} with {drink}");
    }

    public void Order(string meal, string dessert, string drink)
    {
        Console.WriteLine($"Order placed: {meal} with {dessert} and {drink}");
    }
}

class Program
{
    static void Main()
    {
        Restaurant order = new Restaurant();

        order.Order("Coffee");  // Calls 1st method
        order.Order("Burger", "Soda");  // Calls 2nd method
        order.Order("Pizza", "Cake", "Juice");  // Calls 3rd method
    }
}
				
			
🎯 Output:
				
					Order placed: Coffee  
Order placed: Burger with Soda  
Order placed: Pizza with Cake and Juice  
				
			

🧐 Explanation:

1️⃣ Same method name (Order) but different arguments.
2️⃣ If only drink β†’ Calls the first method.
3️⃣ If meal & drink β†’ Calls the second method.
4️⃣ If meal, dessert & drink β†’ Calls the third method.
5️⃣ No confusionβ€”the compiler picks the right method!

πŸ’‘ This makes your code more flexible and readable! 🎯

πŸš€ Another Example: Student Admission System

Let’s create an admission system where:

➑️ Students can register with just their name.
➑️ They can register with name and age.
➑️ They can register with name, age, and course.

				
					using System;

class Student
{
    public void Register(string name)
    {
        Console.WriteLine($"Student {name} registered.");
    }

    public void Register(string name, int age)
    {
        Console.WriteLine($"Student {name}, Age {age} registered.");
    }

    public void Register(string name, int age, string course)
    {
        Console.WriteLine($"Student {name}, Age {age} registered for {course}.");
    }
}

class Program
{
    static void Main()
    {
        Student student = new Student();

        student.Register("Alice");
        student.Register("Bob", 20);
        student.Register("Charlie", 22, "Computer Science");
    }
}
				
			
🎯 Output:
				
					Student Alice registered.  
Student Bob, Age 20 registered.  
Student Charlie, Age 22 registered for Computer Science.  
				
			

πŸ’‘ Why is Method Overloading Important?

βœ… Code looks clean β†’ No need for different function names like AddInt(), AddDouble().
βœ… Easy to use β†’ Same function name with different arguments.
βœ… Improves readability β†’ Users don’t need to remember multiple method names.
βœ… More flexible β†’ The same method can handle different input types.

🎯 Conclusion

Boom! πŸŽ‰ You did it! Now you know Compile-time Polymorphism in C# and how Method Overloading makes code cleaner, flexible, and reusable.

  • You saw how the same method name can handle different parameters.
  • You learned with simple examples, a real-world scenario, and even a restaurant ordering system! πŸ”β˜•

So, the next time you need a method that works with different types of inputs, remember Method Overloading! πŸš€

Β 

⏭️ Next What?

Great job! πŸ™Œ Now, let’s take polymorphism to the next level with Runtime Polymorphism (Method Overriding) in C#! πŸ”₯ Stay tuned! 😊

Β 

πŸ”₯ Challenge for You!

Try creating a Print() method that:

βœ” Prints an integer
βœ” Prints a double
βœ” Prints a string

Let me know if you need help! 😊

Leave a Comment

Share this Doc

Compile-time Polymorphism (Method Overloading)

Or copy link