C# Console.WriteLine(ArrayList) 错误输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10624872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:33:42  来源:igfitidea点击:

Console.WriteLine(ArrayList) wrong output

c#arraylistconsole.writeline

提问by Defi

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.

我正在尝试打印各种 foreach 循环的 ArrayList 的内容,但我唯一得到的是 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

The nodes of MDG are:System.Collections.ArrayList

Can please someone tell me why?

可以请有人告诉我为什么吗?

采纳答案by Kishore Kumar

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

你必须遍历 arraylist 来获取它的值......

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

回答by BrokenGlass

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

转换为字符串 fornodeList只会调用nodeList.ToString()它产生您看到的输出。相反,您必须遍历数组并打印每个单独的项目。

Alternatively you can use string.Join:

或者,您可以使用string.Join

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

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

顺便说一句,没有理由(或借口)仍在ArrayListC# 2 及更高版本中使用 - 如果您不维护遗留代码,请切换到List<T>

回答by Steven Doggart

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

回答by Kvam

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

这会调用 nodeList.ToString()。对列表中的每个元素运行 ToString() 并将它们连接在一起会更有意义:

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

回答by Joel Coehoorn

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

首先,没有充分的理由在 C# 中使用 ArrayList。你应该至少使用System.Collections.Generic.List<T>代替,而即使在这里,他们可能是一个更具体的数据结构中可用。永远不要使用像 ArrayList 这样的无类型集合。

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

其次,当您将对象传递给 Console.Writeline() 时,它只会调用对象的 .ToString() 方法。

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

ArrayList 不会覆盖从基础对象类型继承的 .ToString() 方法。

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.

基本对象类型的 .ToString() 实现只是打印出对象的类型。因此,您发布的行为正是预期的。

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.

我不知道选择不覆盖数组和其他序列类型的 .ToString() 背后的原因,但简单的事实是,如果您希望它打印出数组中的各个项目,您必须将代码编写为迭代这些项目并自己打印它们。

回答by Defi

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.

其中 componentsList 是我想要打印的数组列表。

Thank you all for your help

谢谢大家的帮助