什么是的string.join需要采取一个数组,而不是将IEnumerable的原因是什么? [英] What is the reason string.Join needs to take an array instead of an IEnumerable?
问题描述
正如标题所说:为什么的string.join
需要采取一个数组,而不是IEnumerable的?这使讨厌我,因为我必须添加一个.ToArray()时,我需要建立从LINQ表达式的结果加入字符串。
As the title says: Why does string.Join
need to take an array instead of an IEnumerable? This keeps annoying me, since I have to add a .ToArray() when I need to create a joined string from the result of a LINQ expression.
我的经验告诉我,我缺少明显的东西在这里。
My experience tells me that I'm missing something obvious here.
推荐答案
升级到.NET 4.0,并使用的超载接受一个的IEnumerable<串GT;
。否则,只是接受,这是没有解决之前,.NET 4.0长期悬而未决的问题。您可以通过太创建自己的扩展方法解决这个问题!
Upgrade to .NET 4.0 and use the overload that accepts an IEnumerable<string>
. Otherwise, just accept that it was a long outstanding problem that wasn't addressed until .NET 4.0. You can fix the problem by creating your own extension method too!
public static class StringEnumerableExtensions {
public static string Join(this IEnumerable<string> strings, string separator) {
return String.Join(separator, strings.ToArray());
}
}
用法:
Usage:
IEnumerable<string> strings;
Console.WriteLine(strings.Join(", "));
这篇关于什么是的string.join需要采取一个数组,而不是将IEnumerable的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!