我经常发现自己编写这样的代码:
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);我怀疑有一些聪明的方法可以做到这一点,但我不认为它。有谁有比第一块更好的解决方案吗?
发布于 2008-09-09 21:22:46
执行以下操作:
list.ForEach(i => Console.Write("{0}\t", i));编辑:对于已经回复的其他人-他希望它们都在同一行上,并在它们之间添加制表符。:)
发布于 2008-09-09 21:33:05
一种不同的方法,只是为了好玩:
Console.WriteLine(string.Join("\t", list));发布于 2009-06-04 11:29:50
如果有一段代码你一直在重复,根据“不要重复自己”,你应该把它放在你自己的库中,然后调用它。考虑到这一点,这里有两个方面来获得正确的答案。第一个是调用库函数的代码的清晰度和简洁性。第二个是foreach的性能含义。
首先,让我们考虑一下调用代码的清晰度和简洁性。
您可以通过多种方式执行foreach操作:
在所有使用lamba执行foreach List.ForEach的方法中,是最清晰、最简短的。
list.ForEach(i => Console.Write("{0}\t", i));因此,在这个阶段,List.ForEach可能看起来是一条必由之路。然而,它的性能如何呢?确实,在这种情况下,写入控制台的时间将控制代码的性能。当我们了解某个特定语言特性的性能时,我们至少应该考虑它。
根据Duston Campbell's performance measurements of foreach的说法,在优化代码下迭代列表的最快方法是使用一个不调用List.Count的for循环。
但是,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();
}这段代码将在很多地方使用,因此我们应该进行以下改进:
下面是该函数的代码:
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();
}
}
}=======================================================
你可能会认为这应该是答案的结束。然而,还可以做进一步的泛化。从胖猫的问题中还不清楚他是否一直在给控制台写信。也许在森林里还有其他的事情要做。在这种情况下,Jason Bunting的答案将给出这种一般性。以下是他的回答:
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();
}这允许我们使用尽可能快的迭代method.对集合中的每个元素执行任意代码
我们甚至可以将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();
}
}
}https://stackoverflow.com/questions/52927
复制相似问题