I have a List containing several objects. Is there any way I can print out just one of the objects?
Console.WriteLine(myList.ElementAt(1));
This line of code does not give me the object. It does however give me the name of the Class.
I have a List containing several objects. Is there any way I can print out just one of the objects?
Console.WriteLine(myList.ElementAt(1));
This line of code does not give me the object. It does however give me the name of the Class.
You have two ways.
Access the properties separately
var element = myList.ElementAt(1);
Console.WriteLine("ID:{0}, Name:{1}", element.ID, element.Name);
or overload the ToString()
for the class
public class myObject
{
public int ID { get; set; }
public string Name { get; set; }
public override string ToString()
{
return string.Format("ID:{0}, Name:{1}", ID, Name);
}
}
so you can
Console.WriteLine(myList.ElementAt(1));