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 You Are Going to Learn in This Lesson
βοΈ What is Compile-time Polymorphism in C#?
βοΈ Why is Method Overloading in C# important?
βοΈ How to use it with simple examples.
βοΈ A real-world example for better understanding.
βοΈ 3 different programming examples to make it crystal clear!
βοΈ A friendly code explanation with output.
π§ 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! π