Method Overloading in C# Programming


Introduction

Method overloading in C# allows you to define multiple methods with the same name, but with different parameter types or a different number of parameters. This feature enhances code readability and enables you to perform similar operations with varying input types.

What is Method Overloading?

Method overloading is a concept where you can have multiple methods in the same class with the same name but different signatures. The method signature differs by the number or type of parameters, or both.

When you call an overloaded method, the C# compiler determines which version of the method to execute based on the number and types of the arguments passed.

Syntax of Method Overloading

Here’s the general syntax of method overloading:

    return_type MethodName(parameter1_type parameter1, parameter2_type parameter2, ...)
    {
        // Method body
    }
        

The method name remains the same, but the parameters differ in type or number.

Example 1: Simple Method Overloading

In this example, we will define a method called Add that adds numbers, but with different types of parameters (int, double, etc.).

    using System;
   class Program
    {
        static void Main()
        {
            Console.WriteLine(Add(5, 3));           // Calls Add(int, int)
            Console.WriteLine(Add(5.5, 3.2));       // Calls Add(double, double)
        }
       // Overloaded Add method with int parameters
        static int Add(int a, int b)
        {
            return a + b;
        }
       // Overloaded Add method with double parameters
        static double Add(double a, double b)
        {
            return a + b;
        }
    }
        

Output:

    8
    8.7
        

Explanation

In this example, we have two methods with the same name Add but with different parameter types (int and double). The compiler selects the appropriate method based on the type of the arguments passed. When we pass integers, the method that takes integers is called, and when we pass doubles, the method that takes doubles is called.

Example 2: Overloading with Different Number of Parameters

In this example, we will overload the method by changing the number of parameters.

    using System;
   class Program
    {
        static void Main()
        {
            Console.WriteLine(Add(5, 3));         // Calls Add(int, int)
            Console.WriteLine(Add(5, 3, 2));      // Calls Add(int, int, int)
        }
       // Overloaded Add method with two int parameters
        static int Add(int a, int b)
        {
            return a + b;
        }
       // Overloaded Add method with three int parameters
        static int Add(int a, int b, int c)
        {
            return a + b + c;
        }
    }
        

Output:

    8
    10
        

Explanation

Here, we have overloaded the Add method by changing the number of parameters. The first method accepts two parameters, while the second accepts three parameters. The appropriate method is called based on the number of arguments passed in the method call.

Example 3: Overloading with Optional Parameters

You can also overload methods by using optional parameters. This allows you to create methods that can be called with different numbers of arguments without explicitly overloading them.

    using System;
   class Program
    {
        static void Main()
        {
            Console.WriteLine(Add(5, 3));          // Calls Add(int, int)
            Console.WriteLine(Add(5, 3, 2));       // Calls Add(int, int, int)
            Console.WriteLine(Add(5));             // Calls Add(int)
        }
       // Overloaded Add method with two int parameters
        static int Add(int a, int b)
        {
            return a + b;
        }
       // Overloaded Add method with three int parameters
        static int Add(int a, int b, int c)
        {
            return a + b + c;
        }
       // Overloaded Add method with one int parameter
        static int Add(int a)
        {
            return a + 10; // Adds 10 to the value of a
        }
    }
        

Output:

    8
    10
    15
        

Explanation

In this case, we have three versions of the Add method, with different numbers of parameters. The method with one parameter adds 10 to the passed value. The method with two parameters adds the two values, and the method with three parameters adds all three values. The appropriate method is called based on the number of arguments provided.

Method Overloading Rules

  • The return type alone does not differentiate overloaded methods. Methods must differ in the number or type of parameters.
  • The method signature is determined by the method name and the number, type, or order of its parameters.
  • Method overloading does not depend on return type. For example, you cannot have two methods with the same name but different return types alone.

Conclusion

Method overloading is a powerful feature in C# that allows you to use the same method name for different operations. You can overload methods by changing the number or type of parameters, or by using optional parameters. This helps to make your code more readable and maintainable while allowing you to perform similar operations with different inputs.




Advertisement