Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not call the method with right parameter mapping

Tags:

c#

I have multiple overloaded methods. But I cannot call the right one. How to tell the compiler that I want especially "THIS METHOD" to be called "WITH THIS PARAMETERS"?

The naughty method is the second one:

public string Translate(string text, params object[] args)
{
    // Blah blah blah...
}

public string Translate(string text, string category, params object[] args)
{
    // Here we do some blah blah blah again...
}

Here when I try to call the first method like this: Translate("Hello {0} {1}", "Foo", "Bar"); the compiler assumes that I'm calling the second method and sets the arguments as category = "Foo" and args = "Bar".

I tried to name the parameters while calling them but it gave me some compiler errors.

Translate("Hello {0} {1}", args: "Foo", "Bar"); // CS1738
Translate("Hello {0} {1}", args: "Foo", args: "Bar"); // CS1740

How can I achieve this?

like image 733
Yves Avatar asked Feb 13 '23 09:02

Yves


1 Answers

To make it short: compiler found an exact match so it's preferred (the one with an argument named category) over the more generic one (as Lippert said about this: "closer is always better than farther away"). For a more general discussion see his answer here on SO about overload resolution with null values.

You may pass an array (instead of single values) like:

Translate("Hello {0} {1}", new string[] { "Foo", "Bar" });

This will match first overload because string[] isn't a string (then 2nd overload doesn't apply) and compiler automatically translate an array to params arguments (if types match).

As alternative you may transform second parameter to something that isn't an exact match (then more generic version will be used):

Translate("Hello {0} {1}", (object)"Foo", "Bar");

In general I would avoid this kind of overloads exactly for this reason. It would be much better to use different names for that functions because when arguments type is object things may become not so obvious and such errors may be subtle and you may get wrong results even if compiler doesn't complain.

like image 192
Adriano Repetti Avatar answered Feb 16 '23 01:02

Adriano Repetti