I need to print out the content of this Generic List with console writeline if is possible .Thanks in advance for your help. This question is about this specific piece of code ,I did many search but without luck. code :
using System;
using System.Collections.Generic;
public class Program
{
public class Proto
{
public string name {get;set;}
public Object[] data {get;set;}
}
public void Main()
{
metodo().ForEach(item => Console.Write(item + ","));
}
public List<Proto> metodo()
{
Proto[] myproto = new Proto[5];
List<Proto> mylist = new List<Proto>();
for(int i = 0;i<5;i++)
{
myproto[i] = new Proto {name = "blahblah",data = new Object[]{"dog","cat",2,3}};
mylist.Add(myproto[i]);
}
int h = mylist.Count;
Console.WriteLine("{0}\t",h);
return mylist;
}
}## Heading ##
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));