Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Methods: params string[] with one parameter and single string

This comes from this other question: ( How to build a method with unspecified amount of params en C# ). But since it's a different question I had to ask it here

Supposing you have a method overloaded (this overload is allowed by the compiler):

private static string AddURISlash(params string[] remotePaths) //multiple strings
private static string AddURISlash(string remotePaths) //single string

How should you know which will be executed when only one parameter is recieved?

Is there a convention? or something that you have to test once? Do I have to assume that since the only way to the single string method to be executed is to recieve a single string, that's the one that unequivocally be executed?

Thanks

like image 294
apacay Avatar asked Feb 22 '23 07:02

apacay


1 Answers

How should you know which will be executed when only one parameter is recieved?

You read the specification, which explains how overload resolution is handled. From section 7.5.3.2, the relevant bullet point is:

Otherwise, if MP is applicable in its normal form and MQ has a params array and is applicable only in its expanded form, then MP is better than MQ.

So the version which doesn't require parameter array expansion (your single string version) is chosen at compile-time instead of the parameter array version.

like image 170
Jon Skeet Avatar answered Feb 25 '23 04:02

Jon Skeet