String.Join doesn't accept IEnumerable

Go To StackoverFlow.com

1

I have a method with this signature:

IEnumerable<string> GetCombinations(string s, int length)

And I am trying to use it with string.Join like this:

var combinations = GetCombinations(text, 2);
string result = string.Join(", ", combinations);

But I get the following compiler error:

cannot convert from 'System.Collections.Generic.IEnumerable<string>' to 'string[]'

Can't string.Join take an IEnumerable<string>?

2012-04-04 23:42
by NoName
@MitchWheat How would that solve anything? It would make matters even worse ;-) String.Join expects a string-array, not a string, as second argument; exactly what the compiler is telling the user with that specific error - RobIII 2012-04-04 23:47
steady on. I meant to say ToArray() (which is why I quickly deleted the comment - Mitch Wheat 2012-04-05 00:15


5

Call .ToArray on it?

String.Join(", ",combinations.ToArray());

EDIT

Also see Dan J's answer: Since .Net 4 an overload of String.Join does accept an IEnumerable.

2012-04-04 23:45
by RobIII
Excellent!.İt's work.You saved my day. - NoName 2012-04-04 23:47


7

What version of the .NET Framework are you using? The overload of String.Join that accepts an IEnumerable instead of an array was added in .NET 4.

2012-04-04 23:45
by Dan J
Ads