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

I was looking through the documentation and noticed that the Console.WriteLine() method had several overloads. In particular, my curiosity and partial confusion relate to them:

 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); 

Seems redundant. What do the other four overloads need above the first? The first method is capable of doing everything that other methods can do. Is there a performance issue that they tried to solve by providing extra overloads that handle up to four arguments (the latter)? Is overhead an affordable array of up to four arguments sufficient to handle these overloads?

+86
c #
Mar 07 '14 at 5:44
source share
5 answers

In general, you are right that the first overload may be sufficient for other overloads. This is not entirely true, though, since the params keyword cannot be used for indirect cases, such as binding a group of methods. for example

 delegate void E(string format, object o1); E e = Console.WriteLine; 

Overloading params will not satisfy this case, it will work only when this particular overloading is present.

 public static void WriteLine(string format, object arg0); 

This is a rather esoteric case. More important reasons are the following

  • Not every CLI language is required to support the params keyword. Overloading reduces the load on these languages, eliminating the need to manually create an array for a simple call to WriteLine`
  • Performance. The params overload call causes the caller to allocate an array, even if it is implicitly compiled. Distribution is cheap in .Net, but not for free. Small things like this add up quickly, especially on commonly called methods like Console.WriteLine . The presence of other overloads allows ordinary cases to avoid this distribution.
+103
Mar 07 '14 at 6:10
source share

Overloads are intended for the convenience of C ++ / CLI programs, where the params keyword does not exist.

+37
Mar 07 '14 at 6:02
source share

I think you guys forget that parameters were introduced in C # 2.0. Therefore, overloads also exist from .NET 1.1 when the params keyword did not execute.

+4
Mar 08 '14 at 10:50
source share

I think the question asked already contains good and explanatory answers from JaredPar and jaket , but one point that I think may be appropriate,

I believe that ease of use and freedom for users to use any of the above functions in accordance with this requirement is much more convenient there, and then impose them on the creation of an array when they really do not require it.

I also think about the old days, when I started to learn C # , I almost did not use arrays , and using arrays was a difficult task for me, assign it and then initialize using the correct values. really complicated and time consuming too ...

+1
Mar 07 '14 at 7:10
source share

This does not apply to the performance issue as such. However, increased usability is a good reason for this.

The code below will give you a little information.

 public class TipCalculator { private const double tipRate = 0.18; public static int Main(string[] args) { double billTotal; if (args.Length == 0) { Console.WriteLine("usage: TIPCALC total"); return 1; } else { try { billTotal = Double.Parse(args[0]); } catch(FormatException) { Console.WriteLine("usage: TIPCALC total"); return 1; } double tip = billTotal * tipRate; Console.WriteLine(); Console.WriteLine("Bill total:\t{0,8:c}", billTotal); Console.WriteLine("Tip total/rate:\t{0,8:c} ({1:p1})", tip, tipRate); Console.WriteLine(("").PadRight(24, '-')); Console.WriteLine("Grand total:\t{0,8:c}", billTotal + tip); return 0; } } } 

Please refer to the link: http://msdn.microsoft.com/en-us/library/aa324774(v=vs.71).aspx for more information.

-3
Mar 07
source share



All Articles