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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With