The problem you have is that you are trying to print the whole item.
You can do that if you implement a ToString() method (like in the answer above by JHBonarius) in your "item" class and then call :
foreach (item element in myItem)
{
Console.WriteLine(element.ToString()); .
}
Or you try to print the exact propertiest of your "item" like:
foreach (item element in myItem)
{
Console.WriteLine("x: " + element.x.ToString() + " s: " element.s.ToString());
}
And definitely change your class definition to the one @SteeBono recommends above .
public class item
{
public int x { get; set; }
public string s { get; set; }
}
In case you want to print out your properties dynamically you can use iterate through all of the properties of you class ("item" in this case) :
foreach (var element in myItem)
{
elementPrintData += "Element : \n";
foreach (var fromProp in typeof(item).GetProperties())
{
var toProp = typeof(item).GetProperty(fromProp.Name);
var toValue = toProp.GetValue(element, null);
string val = "";
if (toValue != null)
{
// this will try to print DateTime if present, might need to add null check
if (toProp.PropertyType == typeof(DateTime?) || toProp.PropertyType == typeof(DateTime))
val = ((DateTime)toValue).ToString("dd/MM/yyyy");
else
val = toValue.ToString();
}
// this will print the property name with the value (int, string, bool, double...)
//if it is not a collection/list
// each property will go into a new line because of the " \n"
if (!string.IsNullOrEmpty(val) && !val.Contains("System.Collections"))
elementPrintData += toProp.Name + ": " + val + " \n";
}
}