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

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

问题描述

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

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}	", i.ToString()); }
Console.WriteLine();

最好是这样的:

List<int> list = new List<int> { 1, 3, 5 };
Console.WriteLine("{0}	", 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?

推荐答案

这样做:

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

<小时>

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


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

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

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