Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
392 views
in Technique[技术] by (71.8m points)

c# - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...