Reputation: 91
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?
Upvotes: 2
Views: 35211
Reputation: 21
You can use string.join, while coping the ArrayList to a new regular array by doing this:
Console.WriteLine(string.Join(", ", nodeList.ToArray()));
Upvotes: 0
Reputation: 91
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
Upvotes: 0
Reputation: 416111
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.
Upvotes: 4
Reputation: 2218
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));
Upvotes: 0
Reputation: 43743
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());
Upvotes: 0
Reputation: 12884
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);
Upvotes: 3
Reputation: 160982
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>
Upvotes: 7