C# Member Overloading

C# Member Overloading

Having two or more methods with the same name but different 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

Key Points of Method Overloading in the Context of OOP

1. Same Method Name:

In object-oriented programming (OOP), method overloading allows multiple methods within a class to share the same name. This is known as method name overloading.

2. Different Parameters:

Overloaded methods must differ in their parameter lists. This difference can be in the number of parameters, their types, or both. This is essential in method overloading to distinguish between different method signatures.

3. Compile-Time Polymorphism:

Method overloading is a form of polymorphism in OOP, specifically compile-time (or static) polymorphism. This means that the decision of which method to call is determined by the compiler based on the number and types of parameters passed to the method during compile time.

Scenario: Imagine you are in a kitchen where you need to prepare different dishes by mixing ingredients. Sometimes you mix two ingredients, like flour and sugar, using a hand mixer. Other times, you mix three ingredients, like flour, sugar, and eggs, using a stand mixer. Your task is to write a program that is similar to this given scenario using method overloading.

Example:

using System;
class Kitchen
{
    public void MixIngredients(string ingredient1, string ingredient2)
    {
        Console.WriteLine($”Mixing {ingredient1} and {ingredient2} with a hand mixer.”);
    }
    public void MixIngredients(string ingredient1, string ingredient2, string ingredient3)
    {
        Console.WriteLine($”Mixing {ingredient1}, {ingredient2}, and {ingredient3} with a stand mixer.”);
    }
}
class Program
{
    static void Main(string[] args)
    {
        Kitchen kitchen = new Kitchen();
        kitchen.MixIngredients(“Flour”, “Sugar”);
        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 Kitchen

Defines a new class named Kitchen.

public void MixIngredients(string ingredient1, string ingredient2)

Defines a method to mix two ingredients.

Console.WriteLine($”Mixing {ingredient1} and {ingredient2} with a hand mixer.”);

This will print in console by using interpolation format-“Mixing Flour and Sugar with a hand mixer”

public void MixIngredients(string ingredient1, string ingredient2, string ingredient3)

Overloads the method to mix three ingredients.

Console.WriteLine($”Mixing {ingredient1}, {ingredient2}, and {ingredient3} with a stand mixer.”);

This will print in console by using interpolation format-“Mixing Flour , Sugar and Eggs with a stand mixer

class Program

Defines a new class named Program.

static void Main(string[] args)

Main method, the entry point of the program.

Kitchen kitchen = new Kitchen();

Creates a new Kitchen object to acces kitchen class methods.

kitchen.MixIngredients(“Flour”, “Sugar”);

Calls the method to mix two ingredients.

kitchen.MixIngredients(“Flour”, “Sugar”, “Eggs”);

Calls the overloaded method to mix three ingredients.

Task:

1. Basic Overloading:
Write a class Calculator with two overloaded methods named Add. One method should take two integers as parameters and return their sum, and the other should take three integers.

2. Different Parameter Types:
Create a class Printer with an overloaded method Print. One version should accept a string and print it, and another version should accept an integer and print it.

3. Different Number of Parameters:
Define a class Multiplier with overloaded methods named Multiply. One method should take two double parameters and return their product, and another should take three double parameters.

4. Optional Parameters:
Write a class Concatenator with overloaded methods Concat. One method should concatenate two strings and return the result, and another method should concatenate three strings.

5. Changing Data Types:
Create a class AreaCalculator with an overloaded method CalculateArea. One method should take an integer representing the side length of a square, and another should take two integers representing the length and width of a rectangle.

6. Overloading with Arrays:
Write a class Summation with overloaded methods named Sum. One method should accept an array of integers and return their sum, and another should accept a list of integers and return their sum.

7. Handling Different Input Types: 

Define a class Formatter with overloaded methods named Format. One method should format a double to two decimal places, and another should format a DateTime to a string in “yyyy-MM-dd” format.

8. Parameter Order:
Create a class Logger with overloaded methods Log. One method should accept a string message and a DateTime timestamp, and another should accept a DateTime timestamp and a string message.

9. Overloading with Different Return Types:
Although C# does not support method overloading based solely on return type, write a class Converter with overloaded methods Convert. One method should accept an integer and return its string representation, and another should accept a double and return its string representation.

10. Complex Overloading Scenario:
Write a class Statistics with overloaded methods named CalculateAverage. One method should take an array of integers and return their average as a double, and another should take an array of doubles and return their average as a double.