Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console.WriteLine and generic List

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?

like image 689
Justin R. Avatar asked Sep 09 '08 21:09

Justin R.


2 Answers

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. :)

like image 137
Jason Bunting Avatar answered Oct 04 '22 07:10

Jason Bunting


A different approach, just for kicks:

Console.WriteLine(string.Join("\t", list)); 
like image 34
Vinko Vrsalovic Avatar answered Oct 04 '22 07:10

Vinko Vrsalovic