我经常发现自己编写这样的代码:

List<int> list = new List<int> { 1, 3, 5 };
foreach (int i in list) {
    Console.Write("{0}\t", i.ToString()); }
Console.WriteLine();

更好的是这样的:

List<int> list = new List<int> { 1, 3, 5 };
Console.WriteLine("{0}\t", list);

我怀疑有一些聪明的方法可以做到这一点,但我没有看到。有人有比第一个块更好的解决方案吗?

有帮助吗?

解决方案

做这个:

list.ForEach(i => Console.Write("{0}\t", i));

编辑:对于其他已做出回应的人 - 他希望他们都在同一行上,并且他们之间有制表符。:)

其他提示

另一种方法,只是为了好玩:

Console.WriteLine(string.Join("\t", list));

如果有一段代码您根据“不要重复自己”而一直重复,您应该将其放入您自己的库中并调用它。考虑到这一点,需要从两个方面才能获得正确的答案。首先是调用库函数的代码清晰简洁。第二个是 foreach 的性能影响。

首先让我们考虑一下调用代码的清晰度和简洁性。

您可以通过多种方式执行 foreach:

  1. for循环
  2. foreach循环
  3. 集合.ForEach

在使用 Lamba 执行 foreach List.ForEach 的所有方法中,这是最清晰、最简洁的。

list.ForEach(i => Console.Write("{0}\t", i));

因此,在现阶段,List.ForEach 可能是最佳选择。然而这有什么表现呢?确实,在这种情况下,写入控制台的时间将决定代码的性能。当我们了解某种特定语言功能的性能时,我们当然至少应该考虑它。

根据 Duston Campbell 对 foreach 的性能测量 在优化代码下迭代列表的最快方法是使用 for 循环而不调用 List.Count。

然而 for 循环是一个冗长的结构。它也被视为一种非常迭代的做事方式,与当前函数式惯用语的趋势不相符。

那么我们能否获得简洁、清晰和性能呢?我们可以通过使用扩展方法。在理想的情况下,我们会在控制台上创建一个扩展方法,该方法获取一个列表并使用分隔符写入它。我们不能这样做,因为 Console 是一个静态类,并且扩展方法仅适用于类的实例。相反,我们需要将扩展​​方法放在列表本身上(根据 David B 的建议):

public static void WriteLine(this List<int> theList)
{
  foreach (int i in list)
  {
    Console.Write("{0}\t", t.ToString());
  }
  Console.WriteLine();
}

这段代码将在很多地方使用,所以我们应该进行以下改进:

  • 我们不应该使用 foreach,而应该使用迭代集合的最快方法,即带有缓存计数的 for 循环。
  • 目前只有 List 可以作为参数传递。作为一个库函数,我们可以通过少量的努力来概括它。
  • 使用列表将我们限制为只能使用列表,使用 IList 也允许此代码与数组一起使用。
  • 由于扩展方法将位于 IList 上,因此我们需要更改名称以使其更清楚我们要写入的内容:

该函数的代码如下所示:

public static void WriteToConsole<T>(this IList<T> collection)
{
    int count = collection.Count();
    for(int i = 0;  i < count; ++i)
    {
        Console.Write("{0}\t", collection[i].ToString(), delimiter);
    }
    Console.WriteLine();
}

我们可以通过允许客户端传入分隔符来进一步改进这一点。然后我们可以提供第二个函数,使用标准分隔符写入控制台,如下所示:

public static void WriteToConsole<T>(this IList<T> collection)
{
    WriteToConsole<T>(collection, "\t");
}

public static void WriteToConsole<T>(this IList<T> collection, string delimiter)
{
    int count = collection.Count();
    for(int i = 0;  i < count; ++i)
    {
         Console.Write("{0}{1}", collection[i].ToString(), delimiter);
    }
    Console.WriteLine();
}

现在,鉴于我们需要一种简短、清晰的高性能方式将列表写入控制台,我们已经有了一个。这是完整的源代码,包括使用库函数的演示:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleWritelineTest
{
    public static class Extensions
    {
        public static void WriteToConsole<T>(this IList<T> collection)
        {
            WriteToConsole<T>(collection, "\t");
        }

        public static void WriteToConsole<T>(this IList<T> collection, string delimiter)
        {
            int count = collection.Count();
            for(int i = 0;  i < count; ++i)
            {
                Console.Write("{0}{1}", collection[i].ToString(), delimiter);
            }
            Console.WriteLine();
        }
    }

    internal class Foo
    {
        override public string ToString()
        {
            return "FooClass";
        }
    }

    internal class Program
    {

        static void Main(string[] args)
        {
            var myIntList = new List<int> {1, 2, 3, 4, 5};
            var myDoubleList = new List<double> {1.1, 2.2, 3.3, 4.4};
            var myDoubleArray = new Double[] {12.3, 12.4, 12.5, 12.6};
            var myFooList = new List<Foo> {new Foo(), new Foo(), new Foo()};
            // Using the standard delimiter /t
            myIntList.WriteToConsole();
            myDoubleList.WriteToConsole();
            myDoubleArray.WriteToConsole();
            myFooList.WriteToConsole();
            // Using our own delimiter ~
            myIntList.WriteToConsole("~");
            Console.Read();
        }
    }
}

=======================================================

您可能认为这应该是答案的结尾。然而,还可以进行进一步的概括。从 fatcat 的问题中并不清楚他是否总是在向控制台写入数据。也许需要在 foreach 中做其他事情。在这种情况下,杰森·邦廷的回答将给出这种普遍性。这是他的回答:

list.ForEach(i => Console.Write("{0}\t", i));

除非我们对扩展方法进行进一步的改进并添加 FastForEach,如下所示:

public static void FastForEach<T>(this IList<T> collection, Action<T> actionToPerform)
    {
        int count = collection.Count();
        for (int i = 0; i < count; ++i)
        {
            actionToPerform(collection[i]);    
        }
        Console.WriteLine();
    }

这允许我们对集合中的每个元素执行任意代码 使用尽可能最快的迭代方法.

我们甚至可以更改 WriteToConsole 函数以使用 FastForEach

public static void WriteToConsole<T>(this IList<T> collection, string delimiter)
{
     collection.FastForEach(item => Console.Write("{0}{1}", item.ToString(), delimiter));
}

现在整个源代码,包括 FastForEach 的示例用法是:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleWritelineTest
{
    public static class Extensions
    {
        public static void WriteToConsole<T>(this IList<T> collection)
        {
            WriteToConsole<T>(collection, "\t");
        }

        public static void WriteToConsole<T>(this IList<T> collection, string delimiter)
        {
             collection.FastForEach(item => Console.Write("{0}{1}", item.ToString(), delimiter));
        }

        public static void FastForEach<T>(this IList<T> collection, Action<T> actionToPerform)
        {
            int count = collection.Count();
            for (int i = 0; i < count; ++i)
            {
                actionToPerform(collection[i]);    
            }
            Console.WriteLine();
        }
    }

    internal class Foo
    {
        override public string ToString()
        {
            return "FooClass";
        }
    }

    internal class Program
    {

        static void Main(string[] args)
        {
            var myIntList = new List<int> {1, 2, 3, 4, 5};
            var myDoubleList = new List<double> {1.1, 2.2, 3.3, 4.4};
            var myDoubleArray = new Double[] {12.3, 12.4, 12.5, 12.6};
            var myFooList = new List<Foo> {new Foo(), new Foo(), new Foo()};

            // Using the standard delimiter /t
            myIntList.WriteToConsole();
            myDoubleList.WriteToConsole();
            myDoubleArray.WriteToConsole();
            myFooList.WriteToConsole();

            // Using our own delimiter ~
            myIntList.WriteToConsole("~");

            // What if we want to write them to separate lines?
            myIntList.FastForEach(item => Console.WriteLine(item.ToString()));
            Console.Read();
        }
    }
}

新列表 { 1, 3, 5 }.ForEach(Console.WriteLine);

        List<int> a = new List<int>() { 1, 2, 3, 4, 5 };
        a.ForEach(p => Console.WriteLine(p));

编辑:啊啊,他打败了我。

list.ForEach(x=>Console.WriteLine(x));
List<int> list = new List<int> { 1, 3, 5 };
list.ForEach(x => Console.WriteLine(x));

编辑:该死!花了太长时间打开 Visual Studio 来测试它。

您也可以加入:

var qwe = new List<int> {5, 2, 3, 8};
Console.WriteLine(string.Join("\t", qwe));
public static void WriteLine(this List<int> theList)
{
  foreach (int i in list)
  {
    Console.Write("{0}\t", t.ToString());
  }
  Console.WriteLine();
}

然后,后来……

list.WriteLine();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top