Martin Zikmund
Martin Zikmund

Reputation: 39072

Two methods with the same signature except for optional parameter

I have been working on my project and inadvertently wrote two methods with the same signature except for optional parameters. To my surprise, the code compiles normally.

public async Task<List<CategoryApiModel>> GetCategoriesAsync( int contentLanguageId = 0 )
{
    ...
}

public async Task<List<CategoryApiModel>> GetCategoriesAsync( string languageTag = "" )
{
    ...
}

I know, that when called with empty parameters this will cause the call to be ambiguous, but I am surprised this is even allowed as a declaration itself. Could someone please explain, why is that so?

Upvotes: 2

Views: 1165

Answers (5)

TheLethalCoder
TheLethalCoder

Reputation: 6744

The code will compile fine because both overloads can be called separately by passing the correct parameter into them.

GetCategoriesAsync(1);

So for obvious reasons the code will work.

However if there is a call made, like so: GetCategoriesAsync(), it now throws a compilation error of:

error CS0121: The call is ambiguous

The reason being it stated clearly in the error message, there is no best match for the method as it can easily fit both.

Whats interesting to note is that if there is an empty overload to the function i.e.

public async Task<List<CategoryApiModel>> GetCategoriesAsync()
{}

The code will compile because it will use that call. So the problem can be diverted by doing something like the following:

public async Task<List<CategoryApiModel>> GetCategoriesAsync()
{
     return GetCategoriesAsync(0);
}

Although I would not recommend it.

Upvotes: 1

user836846
user836846

Reputation: 74

Of course, you can use the behaviour of 'No Parameters' for both methods, by passing the default value of the correct type.

E.g.: GetCategoriesAsync(0) for the first method call. GetCategoriesAsync("") for the second method call. So, the behaviour would be same as you don't pass the parameter, since you are passing the default value (Please note, the default value is not the default value of the type, but the default value manually set within the method, if the parameter is not provided)

Upvotes: 0

Dronz
Dronz

Reputation: 2097

There is an issue and there will be a compilation error, but only when some code actually tries to use one of the methods with ambiguous parameters.

This could be useful (if possibly sloppy and confusing) if you had say two files, each one defining one of those two functions. If only one were in scope, then the default parameter could get used, and wouldn't be ambiguous to the compiler. But of course this could be considered sloppy because it might lead to a mistake that might not get noticed.

Upvotes: 1

enkryptor
enkryptor

Reputation: 1663

Actually your program compiles normally until you have at least one GetCategoriesAsync() call. Then you will get "error CS0121: The call is ambiguous".

Upvotes: 2

Tarique Shamim
Tarique Shamim

Reputation: 174

The code will definitely compile normally because you changed the type of the input parameter as String in the second method , this also comes into the signature of a method.This will perform method overloading. Hope this answer your question.

Upvotes: 1

Related Questions