-
July 11th, 2013, 02:55 PM
#1
Print each object in a list.
I'm working on making an interactive periodic table, so I have an Element class, that gets created for each new Element. The element class has a ToString() method. I want the ability to give a quick print out of the elements in the currently list, so I attempted to do a foreach loop, but the following code is only printing what the object is, not the ToString() method in the Element class.
Code:
foreach (object o in elementList)
{
Console.WriteLine("{0}", o.ToString());
}
How can I use the object's ToString() methid, not (I assume) System.Object's?
Note: This is only my 2nd day in C#, so I apologize if this is a simple question. Thanks in advance for any help!
-
July 11th, 2013, 06:47 PM
#2
Re: Print each object in a list.
Use var instead of object.
Code:
foreach (var o in elementList) { ... }
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|