C#的IEnumerable打印出来 [英] C# IEnumerable print out
本文介绍了C#的IEnumerable打印出来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数组,其中我例如想要打印输出在列表中奇数的问题。
I am having problems with an array where I for example want to printout the odd numbers in the list.
int[] numbers = new int[]{ 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
Console.WriteLine(numbers.Where(n => n % 2 == 1).ToArray());
ToString方法似乎不工作?我不想通过元件循环。我该怎么办?
The ToString method does not seem to work? I do not want to loop through the elements. What can I do?
推荐答案
您需要调用的string.join
要创建序列的内容的字符串
You need to call String.Join
to create a string with the contents of the sequence.
例如:
Console.WriteLine(String.Join(", ", numbers.Where(n => n % 2 == 1));
本使用href=\"http://msdn.microsoft.com/en-us/library/dd992421.aspx\">新的重载这需要一个的IEnumerable< T> 结果
在.net 3.5,你需要使用旧的版本,只需要一个字符串。 []
:
This uses the new overload which takes an IEnumerable<T>
.
In .Net 3.5, you'll need to use the older version, which only takes a string[]
:
Console.WriteLine(String.Join(
", ",
numbers.Where(n => n % 2 == 1)
.Select(n => n.ToString())
.ToArray()
)
);
这篇关于C#的IEnumerable打印出来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文