Reputation: 5668
I tried the following code:
class Program: ProgParent
{
public int Max(params int[] op)
{
return 0;
}
public int Max(int i, params int[] op)
{
return 1;
}
public int Max(int i, int j, params int[] op)
{
return 2;
}
public static void Main(string[] args)
{
System.Console.WriteLine((new Program()).Max(5, 6, 7, 8));
System.Console.ReadKey();
}
}
It executes, and uses the most specific function available. But the compiler gives no warning or error about this. Why?
Upvotes: 3
Views: 2876
Reputation: 15867
The C# language spec says:
When performing overload resolution, a method with a parameter array may be applicable either in its normal form [i.e. passing an array] or its expanded form [i.e. passing a variable number of parameters]. The expanded form of a method is available only if the normal form of the method is not available and only if a method with the same signature as the expanded form is not already declared in the same type"
In a (slightly simplified) nutshell: If overload resolution is ambiguous, the compiler chooses the non-params overload.
I guess the reasons for that decision (instead of making code like yours illegal) include:
Upvotes: 4
Reputation: 1062502
Ignoring the build errors (which I'm putting down to typos) - what warning would you expect or want? It is finding a matching overload and using it...
Strictly speaking, I can call different overloads - by passing arrays, but yes, the usage isn't entirely clear.
Without the multiple overloads with params
, this pattern is used quite heavily in things like string.Concat
etc (which underpins +
for strings under the bonnet).
Upvotes: 2