Reputation: 12441
In .net framework I constantly see overloaded functions like the following,
public void Log(string message)
...public void Log(string message, params object[] args)
...my question is since the params keyword allows zero or more parameters, could we just get rid of the first signature? With just the second signature, I could call it with no parameters fine like below, so I don't know why they would have the first signature?
Log("calling with no param");
Upvotes: 5
Views: 1309
Reputation: 30580
There is a small speed advantage as well.
Milliseconds taken for 1 billion iterations of calling a very simple (count++
) method with each:
params
params
Upvotes: 2
Reputation: 43523
Another reason is params
is slow, thinking that all parameters are collected and an array is built. So the second one is slower.
public static string Format(string format, object arg0);
public static string Format(string format, params object[] args);
Upvotes: 6
Reputation: 887479
This pattern is typically used if the array-less version has a simpler implementation.
Upvotes: 2