Console.WriteLine() and the need for so many argument overloads?
Solution 1:
In general you are correct that the first overload can suffice for the other overloads. This is not strictly true though because the params
keyword can't be used for indirect cases like method group binding. For example
delegate void E(string format, object o1);
E e = Console.WriteLine;
The params
overload won't satisfy this case, it will only work when this particular overload is present
public static void WriteLine(string format, object arg0);
That's a pretty esoteric case though. The more important reasons are the following
- Not every CLI language is required to support the
params
keyword. Having the overloads reduces the burden on those languages by removing the need to manually create an array for a simple WriteLine` call - Performance. Calling the
params
overload forces the caller to allocate an array, even if it's done implicitly by the compiler. Allocations are cheap in .Net but not free. Little things like this add up quickly especially on commonly called methods likeConsole.WriteLine
. Having the other overloads allows for the common cases to avoid this allocation
Solution 2:
The overloads are for the convenience of C++/CLI programs where the params keyword doesn't exist.
Solution 3:
I think all of you guys are forgetting that params was introduced in C# 2.0. Therefore, the overloads also exist from .NET 1.1, when the params keyword did not.