There are several examples in the .NET Framework where there are several overloads for the method, some of which use a certain number of parameters, followed by the final "catch all", where the params keyword is used. General examples of this apply to the String class, for example:
I was wondering if there is any specific reason why there are so many method overloads? At first I thought it might be related to performance; question and answers to this question SO - The cost of using parameters in C # - I would suggest.
However, I started to delve into the .NET source code using the Reference Source . I noticed this in the String class source code :
String.Concat() actually runs different code depending on how many fixed arguments are used - this, in my opinion, will definitely be an optimization. String.Format() however seems to provide a wrapper around the main param method - see below for paraphrased code:
public static String Format(String format, Object arg0) { return Format(format, new Object[] { arg0 }); } public static String Format(String format, Object arg0, Object arg1) { return Format(format, new Object[] { arg0, arg1 }); } public static String Format(String format, Object arg0, Object arg1, Object arg2) { return Format(format, new Object[] { arg0, arg1, arg2 }); } public static String Format(String format, params Object[] args) {
Are there any performance benefits, or is it just a matter of convenience, or maybe both? In the specific case above, I do not see any obvious benefit, and this simply duplicates the work.