✔ C# Tutorial
✔ C# Basic
✔ C# Control Statement
✔ C# Function
✔ C# Arrays
✔ C# Object Class
✔ C# Properties
✔ C# Inheritance
✔ C# Polymorphism
✔ C# Abstraction
✔ C# Namespace
✔ C# Exception Handling
✔ C# Collections
✔ C# Misc
C# Member Overloading
If we create two or more members having the same name but different in number or types of parameter, it is known as member overloading. In C#,
we can overload methods, constructors, and indexed properties
It is because these members have parameters only.
Method Overloading in C#
Having two or more methods with the same name but different in parameters, is known as method overloading in C#.
Method overloading allows you to define multiple methods within the same class that share the same name. But don’t be fooled by the identical names! These methods are differentiated by their parameters, creating a way to achieve varied functionalities under the same umbrella
– Improved Readability and Maintainability: By providing multiple methods with the same name but different parameters, you can make your code more readable and intuitive. Developers can easily understand the purpose of each method based on its name and parameters, leading to improved maintainability.
– Flexibility and Convenience: Method overloading allows you to provide multiple ways to perform a task based on different parameter types or numbers. This provides flexibility to callers, as they can choose the most convenient method based on their needs without needing to remember different method names.
Example:
using System;
class Kitchen
{
public void MixIngredients(string ingredient1, string ingredient2)
{
// Method to mix two ingredients using a hand mixer
Console.WriteLine($”Mixing {ingredient1} and {ingredient2} with a hand mixer.”);
}
public void MixIngredients(string ingredient1, string ingredient2, string ingredient3)
{
// Method to mix three ingredients using a stand mixer
Console.WriteLine($”Mixing {ingredient1}, {ingredient2}, and {ingredient3} with a stand mixer.”);
}
}
class Program
{
static void Main(string[] args)
{
Kitchen kitchen = new Kitchen();
// Using a hand mixer to mix two ingredients
kitchen.MixIngredients(“Flour”, “Sugar”);
// Using a stand mixer to mix three ingredients
kitchen.MixIngredients(“Flour”, “Sugar”, “Eggs”);
}
}
Output: Mixing Flour and Sugar with a hand mixer.
Mixing Flour, Sugar, and Eggs with a stand mixer.
Example:
using System;
class CoffeeShop
{
public void MakeCoffee(string size)
{
Console.WriteLine($”Making {size} coffee”);
}
public void MakeCoffee(string size, string type)
{
Console.WriteLine($”Making {size} {type} coffee”);
}
public void MakeCoffee(string size, string type, string flavor)
{
Console.WriteLine($”Making {size} {type} coffee with {flavor} flavor”);
}
}
class Program
{
static void Main(string[] args)
{
CoffeeShop shop = new CoffeeShop();
// Order a regular coffee
shop.MakeCoffee(“Medium”);
// Order a latte
shop.MakeCoffee(“Large”, “Latte”);
// Order a mocha with hazelnut flavor
shop.MakeCoffee(“Small”, “Mocha”, “Hazelnut”);
}
}
Output: Making Medium coffee
Making Large Latte coffee
Making Small Mocha coffee with Hazelnut flavor
Practices Tasks
1. Create a Math class with overloaded methods for basic calculations like Add, Subtract, Multiply, and Divide, allowing them to work with integers and doubles your output should like below.
2. Design a StringManipulator class with overloaded methods for common string operations like Trim, taking either no arguments (trim whitespace) or a character argument (trim specific characters).
For example: First create a class as a stringmanipulator and pass parameter string input and char array input and check if char is null then only trim string otherwise trim char[]. in main method create a variable with inputstring name and assign value then call the trim function with passing input and char[] Your output should like below.
3. Explore how method overloading can enhance the readability and maintainability of your code.