Console.Writeline and Generic List

Console.WriteLine and generic List

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

C# Console writeline print out generics list collection

Override ToString():

public class Proto
{
public string name {get;set;}
public Object[] data {get;set;}
public override string ToString()
{
var dataAsString = data.Aggregate(string.Empty, (current, t) => current + t.ToString());
return name + dataAsString;
}
}

And use it

metodo().ForEach(item => Console.Write(item.ToString() + ","));

EDIT:
If you want it as JSON.
Install Json.Net from Nuget for your project and then

metodo().ForEach(item => Console.Write(JsonConvert.SerializeObject(item) + Environment.NewLine));

Unable to print whole list to console

You are trying to print the object directly to console try iterating over list and print them wherever returned.

for (var item in returned) 
Console.WriteLine(item)

if using a custom Type. keep in mind that you have defined its to string method.



Related Topics



Leave a reply



Submit