C# Console.WriteLine和通用列表

C# Console.WriteLine和通用列表,c#,generics,console,list,C#,Generics,Console,List,我经常发现自己在编写这样的代码: 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", li

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

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);
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();
}
List List=新列表{1,3,5};
foreach(列表中的int i){
Write(“{0}\t”,i.ToString());}
Console.WriteLine();
最好是这样:

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);
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();
}
List List=新列表{1,3,5};
WriteLine(“{0}\t”,列表);
我怀疑有什么聪明的方法可以做到这一点,但我看不出来。有人有比第一个街区更好的解决方案吗

这样做:

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

编辑:对已响应的其他人-他希望他们都在同一行上,并且在他们之间有选项卡。:)

List a=newlist(){1,2,3,4,5};
a、 ForEach(p=>Console.WriteLine(p));
编辑:啊,他赢了我。

List List=新列表{1,3,5};
list.ForEach(x=>Console.WriteLine(x));
list.ForEach(x=>Console.WriteLine(x));

编辑:该死!打开visual studio进行测试花费的时间太长。

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

Console.WriteLine(string.Join("\t", list));
新列表{1,3,5}.ForEach(Console.WriteLine);

如果有一段代码是你一直在重复的,请不要重复你自己,你应该把它放在你自己的库中并调用它。考虑到这一点,这里有两个方面可以得到正确的答案。首先是调用库函数的代码清晰简洁。第二个是foreach的性能影响

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

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

  • for循环
  • foreach循环
  • 收藏
  • 在所有制作foreach列表的方法中,带有lamba的foreach最清晰、最简洁

    list.ForEach(i => Console.Write("{0}\t", i));
    
    因此,在这个阶段,它可能看起来像列表。ForEach是一条路要走。然而,这部电影的表现如何?的确,在这种情况下,写入控制台的时间将控制代码的性能。当我们了解某一特定语言特征的表现时,我们至少应该考虑一下。 根据优化代码,迭代列表的最快方法是使用for循环,而不调用list.Count

    然而,for循环是一个冗长的构造。它还被视为一种非常迭代的方式,与当前功能性习惯用法的趋势不符

    那么我们能做到简洁、清晰和性能吗?我们可以使用一种扩展方法。在理想情况下,我们将在控制台上创建一个扩展方法,该方法接受一个列表并使用分隔符写入。我们不能这样做,因为Console是一个静态类,扩展方法只在类的实例上工作。相反,我们需要将扩展方法放在列表本身上(根据David B的建议):

    除非我们对扩展方法进行进一步改进,并添加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();
        }
    
    publicstaticvoidfastforeach(此IList集合,Action-actionToPerform)
    {
    int count=collection.count();
    对于(int i=0;i
    这允许我们使用最快的迭代方法对集合中的每个元素执行任意代码。

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

    public static void WriteToConsole<T>(this IList<T> collection, string delimiter)
    {
         collection.FastForEach(item => Console.Write("{0}{1}", item.ToString(), delimiter));
    }
    
    公共静态void writeConsole(此IList集合,字符串分隔符)
    {
    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();
            }
        }
    }
    
    使用系统;
    使用System.Collections.Generic;
    使用System.Linq;
    命名空间控制台WriteLineTest
    {
    公共静态类扩展
    {
    公共静态void writeConsole(此IList集合)
    {
    WriteToConsole(集合“\t”);
    }
    公共静态void writeConsole(此IList集合,字符串分隔符)
    {
    FastForEach(item=>Console.Write(“{0}{1}”,item.ToString(),delimiter));
    }
    公共静态void FastForEach(此IList集合,Action-actionToPerform)
    {
    int count=collection.count();
    对于(int i=0;iConsole.WriteLine(item.ToString());
    Console.Read();
    }
    }
    }
    
    您还可以加入:

    var qwe = new List<int> {5, 2, 3, 8};
    Console.WriteLine(string.Join("\t", qwe));
    
    var qwe=新列表{5,2,3,8};
    Console.WriteLine(string.Join(“\t”,qwe));
    
    您必须相对年轻-谁需要Visual Studio?我脑子里一直在想CLR。这是一个非常好的答案!虽然我不喜欢在泛型列表中使用像
    WriteToConsole
    这样的扩展方法。这显然违反了单一责任模式。我不希望列表对控制台有任何了解,无论是在它的实现中还是在扩展方法中。是的,我想您的选择取决于您喜欢什么,以及哪个关注点更大……它不适合您
    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();
            }
        }
    }
    
    list.ForEach(i => Console.Write("{0}\t", i));
    
    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();
        }
    
    public static void WriteToConsole<T>(this IList<T> collection, string delimiter)
    {
         collection.FastForEach(item => Console.Write("{0}{1}", item.ToString(), delimiter));
    }
    
    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();
            }
        }
    }
    
    var qwe = new List<int> {5, 2, 3, 8};
    Console.WriteLine(string.Join("\t", qwe));