Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string.Join - "cannot convert from IEnumerable to string[]"

Tags:

string

c#

.net

linq

Very simple extension method not compiling:

public static string Join(this string text, params string[] stringsToJoin)
{
    return String.Join(", ", stringsToJoin.Where(s => !string.IsNullOrEmpty(s)));
}

I get "cannot convert from 'System.Collections.Generic.IEnumerable' to 'string[]'"

What am I missing?

like image 672
Jim Balo Avatar asked Jul 26 '13 01:07

Jim Balo


Video Answer


1 Answers

The overload of String.Join which accepts an IEnumerable<String> was only added in .NET 4.0. It seems you're compiling against an earlier version.

The easiest way to fix this and make it compatible with .NET 3.5 would be to simply call .ToArray():

public static string Join(this string text, params string[] stringsToJoin)
{
    return String.Join(", ", stringsToJoin.Where(s => !string.IsNullOrEmpty(s))
                                          .ToArray());
}
like image 61
p.s.w.g Avatar answered Oct 15 '22 21:10

p.s.w.g