Cannot call method with correct parameter mapping

I have several overloaded methods. But I can’t name the right one. How can I tell the compiler that I want this "METHOD" to be called "WITH THESE PARAMETERS"?

The naughty method is the second:

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 as follows:, the Translate("Hello {0} {1}", "Foo", "Bar");compiler assumes that I call the second method and sets the arguments as category = "Foo"well args = "Bar".

I tried to name the parameters when they were called, but this 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?

+4
source share
1 answer

: , (, category) ( Lippert : " , " ). . SO null .

( ), :

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

, string[] string ( ), params ( ).

, ( ):

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

, . , , object, , , , .

+8

Source: https://habr.com/ru/post/1533525/


All Articles