Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why could C# overload two methods with the same params as long as one of them has default param?

I recently noticed that C# compiler allows methods overloads like the following:

    public static string foo(string a, bool x = false)
    {
        return "a";
    }
    public static string foo(string a)
    {
        return "b";
    }

As far as I tested, it always returns "b" as long as the second param is not given, which makes sense. However, I think the compiler really should not allow this type of overloading. May I ask the reason why this feature is designed like this rather than giving an error by compiler?

like image 759
bearxy39 Avatar asked Oct 14 '25 05:10

bearxy39


1 Answers

While questions like this are fundamentally impossible to answer, since it is impossible to guess the language designers intentions, I might make a guess.

Optional arguments are handled by transforming the code to inject the argument to the call site. So

public static void Test(int a = 42){...}

...
Test();

would be transformed to to

public static void Test(int a){...}

...
Test(42);

by the compiler. From this point on the regular overload resolution can run without conflicts. Why was it designed this way? I have no idea, but common reasons for non intuitive features is backward compatibility or language compatibility.

For this reason it is important to be very careful using optional arguments in public APIs. Since the user of the library will use the default value from the version of the API it was compiled against. Not the version it is running against.

like image 188
JonasH Avatar answered Oct 16 '25 18:10

JonasH