C # params keyword and overload function

In the .net framework, I constantly see overloaded functions, such as:

  • public void Log(string message)...
  • public void Log(string message, params object[] args)...

My question is that the params keyword allows zero or more parameters, is it possible to just get rid of the first signature? Having only the second signature, I could name it without parameters, as shown below, so I do not know why they will have the first signature?

Log("calling with no param");
+5
source share
3 answers

Another reason: it paramsworks slowly, thinking that all parameters are assembled and the array is built. The second is slower.

public static string Format(string format, object arg0);
public static string Format(string format, params object[] args);
+6
source

This pattern is usually used if the version without an array has a simpler implementation.

+2

.

, 1 (count++) :

  • 2472 params
  • 7773 w/params
+2

All Articles