C# 在许多实现方面存在问题。在运行时编译良好但不是所需的输出
我正在尝试测试MapMany实现,但在获取输出时遇到问题 代码如下:C# 在许多实现方面存在问题。在运行时编译良好但不是所需的输出,c#,ienumerable,C#,Ienumerable,我正在尝试测试MapMany实现,但在获取输出时遇到问题 代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication3 { static class E { static void Main() { var sequence = ne
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
static class E
{
static void Main()
{
var sequence = new int[] { 0, 1, 2, 3, 4 };
var result = sequence.MapMany(
s => s % 2 == 1 ? new int[] { s } : new int[] { });
Console.WriteLine("{0}", result);
}
public static IEnumerable<U> MapMany<T,U>
(this IEnumerable<T> sequence, Func<T,IEnumerable<U>> func) {
foreach (T item in sequence) {
IEnumerable<U> sequence2 = func(item);
foreach (U item2 in sequence2) {
yield return item2;
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间控制台应用程序3
{
静态E类
{
静态void Main()
{
var序列=新的int[]{0,1,2,3,4};
var result=sequence.MapMany(
s=>s%2==1?新的int[]{s}:新的int[]{});
WriteLine(“{0}”,result);
}
公共静态IEnumerable MapMany
(此IEnumerable序列,Func Func){
foreach(按顺序排列的T项){
IEnumerable sequence2=函数(项);
foreach(序列2中的U项2){
收益回报项目2;
}
}
}
}
}
当我尝试运行代码时,它会给我以下输出:
ConsoleApplication3.E+<MapMany>d__2'2[System.Int32,System.Int32]
ConsoleApplication3.E+d__2'2[System.Int32,System.Int32]
有人知道怎么解决这个问题吗
干杯更换控制台。WriteLine。输出是正确的 编辑: result.ToList().ForEach(x=>Console.Write(x+) 例如
result.ToList().ForEach(res=>Console.WriteLine("{0}", res));
它正在执行一个result.ToString(),它基本上输出类型。如果要输出值,需要迭代结果中的值并输出每个值;当您
Console.WriteLine
执行此序列时,您希望看到什么?