string.Join - “cannot convert from IEnumerable to string[]”

自古美人都是妖i 提交于 2021-01-27 03:50:41

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/17871370/string-join-cannot-convert-from-ienumerable-to-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!