Open In App

Method Overloading in C#

Last Updated : 08 Sep, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Method Overloading in C# is the ability to define multiple methods with the same name but different parameter lists.

  • Parameter lists can differ by type, number or order of parameters.
  • Improves readability and lets related tasks use the same method name.
  • Cannot overload methods by only changing the return type (causes compile-time error).
  • Also known as compile-time (static) polymorphism.

Different Ways of Method Overloading

Method overloading can be done by changing:

  • Changing the number of Parameters
  • Changing data types of the parameters.
  • Changing the Order of the parameters.

1. Changing the number of Parameters

We can achieve method overloading by changing the number of parameters of a method.

Example: Overloading by changing the Number of parameters

C#
using System;

class Geeks {
    // adding two integer values.
    public int Add(int a, int b)
    {
        int sum = a + b;
        return sum;
    }

    // adding three integer values.
    public int Add(int a, int b, int c)
    {
        int sum = a + b + c;
        return sum;
    }

    public static void Main(String[] args)
    {
        Geeks ob = new Geeks();

        int sum1 = ob.Add(1, 2);
        Console.WriteLine("add() with two integers");
        Console.WriteLine("sum: " + sum1);

        Console.WriteLine("add() with three integers");
        int sum2 = ob.Add(1, 2, 3);
        Console.WriteLine("sum: " + sum2);
    }
}

Output
add() with two integers
sum: 3
add() with three integers
sum: 6

2. Changing the Data types of the parameters

We can achieve method overloading by changing the data type of the method's parameter.

Example: Overloading by changing the Data types of the parameters

C#
using System;

class Geeks {
    // adding three integer values.
    public static int Add
    {
        int sum = a + b + c;
        return sum;
    }

    // adding three double values
    public static double Add(double a, double b, double c)
    {
        double sum = a + b + c;
        return sum;
    }

    public static void Main(String[] args)
    {
        Console.WriteLine("Add() with integer parameter");

        int sum2 = Add(1, 2, 3);
        Console.WriteLine("sum: " + sum2);

        Console.WriteLine("Add() with double parameter");

        double sum3 = Add(1.0, 2.0, 3.0);
        Console.WriteLine("sum: " + sum3);
    }
}

Output
Add() with integer parameter
sum: 6
Add() with double parameter
sum: 6

3. Changing the Order of the parameters

If the method has the same name but a parameters order of parameters so in this way we can also achieve the method overloading.

Example:

C#
using System;

class Geeks {
    public void Identity(String name, int id)
    {
        Console.WriteLine("Name1 : " + name + ", "
                          + "Id1 : " + id);
    }

    public void Identity(int id, String name)
    {
        Console.WriteLine("Name2 : " + name + ", "
                          + "Id2 : " + id);
    }

    public static void Main(String[] args)
    {
        Geeks obj = new Geeks();

        // by changing the order
        obj.Identity("Geek", 1);
        obj.Identity(2, "Geek2");
    }
}

Output
Name1 : Geek, Id1 : 1
Name2 : Geek2, Id2 : 2

Example: In this example, we understand what happens when the method signature is the same and the return type is different

C#
using System;

class Geeks {
    // adding two integer value.
    public int Add(int a, int b)
    {
        int sum = a + b;
        return sum;
    }

    // adding three integer value.
    public double Add(int a, int b)
    {
        double sum = a + b + 0.0;
        return sum;
    }

    public static void Main(String[] args)
    {
        // Creating Object
        Geeks ob = new Geeks();

        int sum1 = ob.Add(1, 2);
        Console.WriteLine(
            "sum of the two integer " "value :" + sum1);

        int sum2 = ob.Add(1, 2);
        Console.WriteLine(
            "sum of the three integer " "value :" + sum2);
    }
}

Output:

ErrorInOverriding
Output

Explanation: The compiler will give an error as the return value alone is not sufficient for the compiler to figure out which function it has to call. Only if both methods have different parameter types (so, they have different signatures), then Method overloading is possible.


Article Tags :