Search code examples
c#arraylistconsole.writeline

Console.WriteLine(ArrayList) wrong output


I'm trying to print the content of the ArrayList of the various foreach loops but the only thing i get is the String + System.Collections.ArrayList.

For example the following code:

ArrayList nodeList = new ArrayList();
foreach (EA.Element element in elementsCol)
{
    if ((element.Type == "Class") || (element.Type == "Component") || (element.Type == "Package"))
    {
         nodeList.Add(element);
    }
    Console.WriteLine("The nodes of MDG are:" + nodeList); //stampato a schermo la lista dei nodi nel MDG finale

And the output that i get is:

The nodes of MDG are:System.Collections.ArrayList

Can please someone tell me why?


Solution

  • you have to loop through the arraylist to get its value...

    foreach(var item in nodeList)
    {
        Console.WriteLine("The nodes of MDG are:" + item);
    }
    

    This will work..

    Updated:

    Use element instead of nodelist

    Console.WriteLine("The nodes of MDG are:" + element);