Console.WriteLine和通用列表 [英] Console.WriteLine and generic List

查看:170
本文介绍了Console.WriteLine和通用列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常发现自己写的代码如下:

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

Better会是这样:

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

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

解决方案

执行:

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






编辑:对有回应的其他人他们都在同一行,与他们之间的选项卡。 :)


I frequently find myself writing code like this:

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

Better would be something like this:

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

I suspect there's some clever way of doing this, but I don't see it. Does anybody have a better solution than the first block?

解决方案

Do this:

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


EDIT: To others that have responded - he wants them all on the same line, with tabs between them. :)

这篇关于Console.WriteLine和通用列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆