C# Method Overloading

Introduction

Method overloading is a feature in C# that allows a class to have multiple methods with the same name but different parameters. The parameters can differ by the number of parameters, the type of parameters, or both. Method overloading helps to enhance the readability and maintainability of the code by allowing multiple methods to perform similar operations with different inputs.

How Method Overloading Works

When you call an overloaded method, the compiler determines which version of the method to invoke based on the number and types of the arguments you pass. This process is known as compile-time polymorphism.

Example

Let’s consider an example to demonstrate method overloading.

using System;

namespace MethodOverloadingExample
{
    public class Calculator
    {
        // Method to add two integers
        public int Add(int a, int b)
        {
            return a + b;
        }

        // Overloaded method to add three integers
        public int Add(int a, int b, int c)
        {
            return a + b + c;
        }

        // Overloaded method to add two doubles
        public double Add(double a, double b)
        {
            return a + b;
        }

        // Overloaded method to add three doubles
        public double Add(double a, double b, double c)
        {
            return a + b + c;
        }

        // Overloaded method to add an array of integers
        public int Add(params int[] numbers)
        {
            int sum = 0;
            foreach (int number in numbers)
            {
                sum += number;
            }
            return sum;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Calculator calc = new Calculator();

            // Calling overloaded methods
            Console.WriteLine("Sum of 5 and 3: " + calc.Add(5, 3));
            Console.WriteLine("Sum of 5, 3, and 2: " + calc.Add(5, 3, 2));
            Console.WriteLine("Sum of 5.5 and 3.3: " + calc.Add(5.5, 3.3));
            Console.WriteLine("Sum of 5.5, 3.3, and 2.2: " + calc.Add(5.5, 3.3, 2.2));
            Console.WriteLine("Sum of array {1, 2, 3, 4}: " + calc.Add(1, 2, 3, 4));
        }
    }
}

Output

Sum of 5 and 3: 8
Sum of 5, 3, and 2: 10
Sum of 5.5 and 3.3: 8.8
Sum of 5.5, 3.3, and 2.2: 11.0
Sum of array {1, 2, 3, 4}: 10

Key Points

  1. Same Method Name: All overloaded methods must have the same name.
  2. Different Parameters: The methods must differ in the number of parameters, the type of parameters, or both.
  3. Return Type: The return type of the methods can be the same or different. However, the return type alone cannot be used to distinguish overloaded methods.

Benefits of Method Overloading

  1. Readability: Method overloading improves code readability by allowing methods with the same name to handle different types of inputs.
  2. Maintainability: It makes the code easier to maintain and understand, as related methods are grouped together by name.
  3. Flexibility: It provides flexibility in calling methods with different sets of parameters, enhancing the usability of the methods.

Common Use Cases

  1. Mathematical Operations: Methods to perform arithmetic operations on different types of numbers (integers, doubles, etc.).
  2. String Manipulations: Methods to manipulate strings with different types of inputs (e.g., concatenation, substring extraction).
  3. Input/Output Operations: Methods to read/write data from/to different sources (e.g., files, network streams).

Example: Overloading with Different Types

Let’s create another example to demonstrate overloading with different parameter types.

using System;

namespace OverloadingWithDifferentTypes
{
    public class Display
    {
        // Method to display an integer
        public void Show(int number)
        {
            Console.WriteLine("Integer: " + number);
        }

        // Overloaded method to display a double
        public void Show(double number)
        {
            Console.WriteLine("Double: " + number);
        }

        // Overloaded method to display a string
        public void Show(string message)
        {
            Console.WriteLine("String: " + message);
        }

        // Overloaded method to display a boolean
        public void Show(bool flag)
        {
            Console.WriteLine("Boolean: " + flag);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Display display = new Display();

            // Calling overloaded methods
            display.Show(42);
            display.Show(3.14);
            display.Show("Hello, world!");
            display.Show(true);
        }
    }
}

Output

Integer: 42
Double: 3.14
String: Hello, world!
Boolean: True

Conclusion

Method overloading in C# allows you to define multiple methods with the same name but different parameters. This enhances the flexibility, readability, and maintainability of your code. By understanding and using method overloading effectively, you can create more intuitive and versatile classes and methods in your applications.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top