28

I want to pass an array of custom objects to a function like String.Join which has the following signatures:

  • public static string Join(string separator, params Object[] values)
  • public static string Join(string separator, IEnumerable<T> values)

If I call the function like this:

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

I get a compiler error:

The call is ambiguous between the following methods or properties: 'string.Join(string, params object[])' and 'string.Join(string, System.Collections.Generic.IEnumerable)'

I can resolve the ambiguity by using the IEnumerable<T> function:

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

But can I call the params object[] function?

I am using C# 4.0, if that makes any difference.

bouvierr
  • 3,563
  • 3
  • 27
  • 32
  • 1
    Not using the `params object[]` overload, but while `using System.Linq;` you can then do `string text = string.Join("\n", myArray.AsEnumerable());` – Elaskanator Oct 01 '20 at 02:58

5 Answers5

35

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

cdhowie
  • 158,093
  • 24
  • 286
  • 300
4

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

Wouter de Kort
  • 39,090
  • 12
  • 84
  • 103
1

The simplest change to your code would be to go from:

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

To:

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

As said before, casting also works:

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

To learn more about this subject, research C# overload resolution.

Overload resolution is an interesting subject in its own right, but I have yet to find it to be the bottleneck for a performance problem.

Doug
  • 2,441
  • 2
  • 19
  • 22
  • As I mentioned in my question, I want to pass the array to the appropriate implementation of my own function (not String.Join, but something with similar method signatures). The performance difference here is due to the different implementations of my function. Some operations, like getting the number of elements in the collection, are done faster using an array rather than an `IEnumerable`. – bouvierr Apr 10 '13 at 21:22
  • The Count() extension method uses the .Length proprty in the case where the IEnumerable is an array, but that's not your point. You might want to consider using different names for the members, and not relying on overload resolution, if the performance difference is that critical. Therefore, you won't have to coerce compile-time types to get the semantics that you are looking for. – Doug Apr 12 '13 at 17:49
1

If your are using IEnumerable you can use the <object> generic overload of the ToArray() method:

var allFoos = foo.GetAllFoos().ToArray<object>(); 
string s = string.Join(", ", allFoos); 

Looks less bloated and more readable to me.

Kemuel Sanchez
  • 636
  • 7
  • 13
0

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

string text = string.Join("\n", new MyClass(), new MyClass());
Community
  • 1
  • 1
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185