Overloaded function with string [] parameter in signature

I have two functions

public static string Function1 (string id, params string[]) { return Function1(id, null, null, params) } public static string Function1 (string id, string id2, Object a, params string[]) { string id = id, if (IsValidId(id)) { start = new ProcessStartInfo(); start.Arguments = params; if (string.IsNullOrEmpty(id2)==false) { start.RedirectStandardOutput = true; } } } 

I want to use the second overload when I make the next call

 MyStaticClaass.Function1( input1, input2, null, // (This one represents the Object) input3, input4, input5); 

Is there a way to get him to move on to the second method definition?

Compilation error: this call is ambiguous between the following methods or properties: (and then the two above methods)

PS: I did not select these two functions. I cannot change my signature or their names.

+5
source share
2 answers

You can use Named arguments to overload a specific function, for example

  Program.Function1( id: input1, id2: input2, o:null, array: new string[] {input3, input4, input5}); 

and it will fall into function

 public static string Function1 (string id, string id2, Object o, params string[] array) { } 

for more details you can check this named arguments

+6
source

It could be like this:

 var result = Rootobject.Function1( "", "", null, // (This one represents the Object) new string[] { "", "", ""}); 

However, since you already know that this is a fragile design, or you would not stop asking about it, you might want to reconsider your overloads. Of course, we got it to work, but it's not a very good design.

+1
source

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


All Articles