Print each object in a list.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Print each object in a list.

  1. #1
    Join Date
    Jul 2013
    Posts
    4

    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!

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,197

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center