Passing array to function that takes either params object[] or IEnumerable<T> Passing array to function that takes either params object[] or IEnumerable<T> arrays arrays

Passing array to function that takes either params object[] or IEnumerable<T>


If you pass an object[] as the second parameter, the compiler should choose the object[] overload since it exactly matches. In the case where you have a differently-typed array (MyClass[] in this case) just cast the array to object[]:

string.Join("\n", (object[])arr);

You are not actually changing the types of any objects or performing any conversion at runtime, you're only giving the compiler a hint regarding which overload to use.

And regarding your comment about performance, don't forget to benchmark both options if performance is that critical. Don't assume one is faster than the other. (And always profile your entire application -- it's likely that any bottlenecks are going to be elsewhere.)


If you change the type of your arr variable to object[] you will call the other overload:

object[] arr = new MyClass[] { new MyClass(), new MyClass() };string text = string.Join("\n", arr);

You can also explicitly cast it to object[]: string.Join("\n", (object[])arr);


You can call the other overload like this (that's what param are used for) -

string text = string.Join("\n", new MyClass(), new MyClass());