Pergunta

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?

Foi útil?

Solução

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);

Outras dicas

The conversion to string for nodeList will just call nodeList.ToString() which produces the output you see. Instead you have to iterate over the array and print each individual item.

Alternatively you can use string.Join:

Console.WriteLine("The nodes of MDG are:" + string.Join(",", nodeList));

By the way there is no reason (or excuse) to still use ArrayList in C# 2 and above - if you are not maintaining legacy code switch to List<T>

First of all, there's no good reason to use ArrayList in C#. You should at least use System.Collections.Generic.List<T> instead, and even here they may be a more specific data structure available. Never use an untyped collection like ArrayList.

Second, when you pass an object to Console.Writeline(), it just calls the .ToString() method of the object.

ArrayList does not override the .ToString() method inherited from the base object type.

The .ToString() implementation on the basic object type simply prints out the type of the object. Therefore, the behavior you posted is exactly what is expected.

I don't know the reasoning behind the choice not to override .ToString() for arrays and other sequence types, but the simple fact is that if you want this to print out the individual items in the array, you must write the code to iterate over the items and print them yourself.

StringBuilder builder = new StringBuilder();
foreach (EA.Element element in elementsCol)
{
    if ((element.Type == "Class") || (element.Type == "Component") || (element.Type == "Package"))
    {
        builder.AppendLine(element.ToString());

    }
 }
 Console.WriteLine("The nodes of MDG are:" + builder.ToString());

This calls on nodeList.ToString(). It would make more sense to run ToString() on each element in the list and join them together:

Console.WriteLine("The nodes of MDG are:" + string.Join(", ", nodeList));

I got the output that i wanted with the following code:

using System.IO

using (StreamWriter writer = new StreamWriter("C:\\out.txt"))
        {
            Console.SetOut(writer);
         }

Console.WriteLine("the components are:");
        foreach (String compName in componentsList)
        { Console.WriteLine(compName); }

where componentsList is my arraylist that i wanted to print.

Thank you all for your help

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top