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

This stems from this other question: ( How to create a method with an undefined number of parameters en C # ). But since this is a different question, I should have asked him here.

Suppose you are overloaded with a method (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 to know what will be executed when only one parameter is received?

Is there an agreement? or something that you should check once? Should I assume that since the only way for a single string method to be executed is to get a single string that runs oddly?

thanks

+4
source share
1 answer

How to know what will be executed when only one parameter is received?

You read the spec that explains how overload resolution is handled. From section 7.5.3.2, the corresponding paragraph indicates:

Otherwise, if M P is applicable in its normal form, and M Q has an array of params and is applicable only in its expanded form, then M P is better than M Q.

Thus, a version that does not require expansion of the parameter array (your single-line version) is selected at compile time instead of the version of the parameter array.

+8
source

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


All Articles