Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console.WriteLine() and the need for so many argument overloads?

Tags:

c#

I was browsing through the documentation and noticed that Console.WriteLine() method had several overloads. Particularly, my curiosity and partial confusion pertains to these:

public static void WriteLine(string format, params object[] arg); public static void WriteLine(string format, object arg0); public static void WriteLine(string format, object arg0, object arg1); public static void WriteLine(string format, object arg0, object arg1, object arg2); public static void WriteLine(string format, object arg0, object arg1, object arg2, object arg3); 

It seems redundant. What is the need of the other four overloads on top of the first one? The first method is able to do everything that the other methods can do. Is there a performance concern that they were trying to tackle by providing additional overloads, which handle up to four arguments (last one)? Is the overhead of going through an array of up to four arguments large enough to provide the need for these overloads?

like image 326
B.K. Avatar asked Mar 07 '14 05:03

B.K.


People also ask

How does WriteLine () method works?

WriteLine(String, Object, Object) Writes the text representation of the specified objects, followed by the current line terminator, to the standard output stream using the specified format information.

Why is used in console WriteLine?

While Write() and WriteLine() both are the Console Class methods. The only difference between the Write() and WriteLine() is that Console. Write is used to print data without printing the new line, while Console. WriteLine is used to print data along with printing the new line.

Does console WriteLine affect performance?

WriteLine has an impact on the performance of your specific application is profiling it. Otherwise it's premature optimization. Show activity on this post. If it's for debugging purpose, you should rather use: Debug.

What is the difference between system console WriteLine and console WriteLine?

So, if you're talking about System. Console. WriteLine in both cases, there's no difference.


1 Answers

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

  1. 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
  2. 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 like Console.WriteLine. Having the other overloads allows for the common cases to avoid this allocation
like image 192
JaredPar Avatar answered Sep 24 '22 01:09

JaredPar