我有以下代码

    IEnumerable<int> numbers =
        Enumerable.Range(1, 5)
        .Reverse();
    Func<int, string> outputFormat = x => x + "...";
    IEnumerable<string> countdown = numbers.Select(outputFormat);
    foreach (string s in countdown)
    {
        Console.WriteLine(s);
    }


有没有一种方法可以从代码中“消除” foreach循环,例如

Console.Write(countdown.EnumerateOverItems())


而不实际编写自定义方法(例如使用LINQ或以某种方式委托)?

最佳答案

这应该可以解决问题:

Console.WriteLine(string.Join(Environment.NewLine, countdown));

关于c# - 有没有一种方法可以在一个Console.Write(line)中写入整个IEnumerable <T>?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25203663/

10-17 02:07