string.Join-“无法从IEnumerable转换为string []"; [英] string.Join - "cannot convert from IEnumerable to string[]"
本文介绍了string.Join-“无法从IEnumerable转换为string []";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
非常简单的扩展方法无法编译:
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)));
}
我得到无法从'System.Collections.Generic.IEnumerable'转换为'string []'"
I get "cannot convert from 'System.Collections.Generic.IEnumerable' to 'string[]'"
我想念什么?
推荐答案
的过载仅在.NET 4.0中添加了接受 IEnumerable< String>
的 String.Join
.看来您正在针对早期版本进行编译.
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.
解决此问题并使它与.NET 3.5兼容的最简单方法是简单地调用 .ToArray()
:
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());
}
这篇关于string.Join-“无法从IEnumerable转换为string []";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文