Overloaded method and ambiguity in C#

With method overloading, you can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. However, when using default parameters with overloaded methods, ambiguity can occur if the compiler cannot determine which method to call.

Syntax

Following is the syntax for method overloading −

public void MethodName(int parameter1) {
    // Implementation 1
}

public void MethodName(int parameter1, int parameter2) {
    // Implementation 2
}

When using default parameters, ambiguity can arise −

public void MethodName(int param1 = 10) { }
public void MethodName(int param1, int param2 = 20) { }
// Calling MethodName(5) creates ambiguity

Successful Method Overloading

Let us see an example where method overloading works without ambiguity. In this example, the call would go to the method with a single parameter −

Example

using System;

class Student {
    static void DisplayMarks(int marks1 = 90) {
        Console.WriteLine("Method with one parameter!");
    }

    static void DisplayMarks(int marks1, int marks2 = 95) {
        Console.WriteLine("Method with two parameters!");
    }

    static void Main() {
        DisplayMarks(97);
    }
}

The output of the above code is −

Method with one parameter!

Method Overloading Ambiguity

Now let us see what creates an ambiguous call. Here the confusion is that both methods could potentially match the call DisplayMarks(80). The first method needs one default parameter, and the second method needs two default parameters. This creates ambiguity because the compiler cannot decide which method to invoke.

Example - Compilation Error

using System;

class Student {
    static void DisplayMarks(int marks1 = 90, int marks2 = 80) {
        Console.WriteLine("Method with two parameters!");
    }

    static void DisplayMarks(int marks1, int marks2 = 80, int marks3 = 98) {
        Console.WriteLine("Method with three parameters!");
    }

    static void Main() {
        // This will cause a compilation error due to ambiguity
        // DisplayMarks(80);  // Commented to avoid compilation error
        
        // Clear calls work fine
        DisplayMarks(80, 85);          // Calls first method
        DisplayMarks(80, 85, 95);      // Calls second method
    }
}

The output of the above code is −

Method with two parameters!
Method with three parameters!

Resolving Ambiguity

To resolve ambiguity in method overloading with default parameters, you can −

Using Different Parameter Types

using System;

class Calculator {
    static void Calculate(int a, int b = 10) {
        Console.WriteLine("Integer calculation: " + (a + b));
    }

    static void Calculate(double a, double b = 10.5) {
        Console.WriteLine("Double calculation: " + (a + b));
    }

    static void Main() {
        Calculate(5);       // Calls int version
        Calculate(5.5);     // Calls double version
    }
}

The output of the above code is −

Integer calculation: 15
Double calculation: 16

Comparison of Ambiguous vs Clear Overloading

Ambiguous Overloading Clear Overloading
Multiple methods can match the same call Each method call has a unique match
Compiler cannot determine which method to invoke Compiler can clearly identify the target method
Results in compilation error Compiles and runs successfully
Often caused by overlapping default parameters Uses distinct parameter counts or types

Conclusion

Method overloading with default parameters can create ambiguity when multiple overloaded methods could match a single method call. To avoid this, ensure that overloaded methods have distinct signatures that don't overlap when default parameters are considered, or use different parameter types to distinguish between methods.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements