列表中的列表不打印元素,而是显示 System.Collections.Generic.List`1[System.Object] [英] List inside list does not print elements, instead shows System.Collections.Generic.List`1[System.Object]

查看:41
本文介绍了列表中的列表不打印元素,而是显示 System.Collections.Generic.List`1[System.Object]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含三个项目的列表.第一个元素是整数,第二个也是整数,第三个是另一个列表.通过使用 for 每个循环,我可以打印这些项目.

I have a list which contains three items. First element is integer, second one is also an integer and third one is another list. By using for each loop I'm able to print these items.

List<object> mainList= new List<object>();

mainList.Add(binRead.ReadInt32()); // Reads first integer ant adds to mainList
mainList.Add(binRead.ReadInt32()); // Reads second integer ant adds to mainList

List<object> avlDataList = new List<object>();

for(int i=0; i<n; i++)
{
  // Reads some data and adds it to avlDataList
}

mainList.Add(avlDataList); // Adds that list to mainList

foreach(var i in mainList)
{
   Console.WriteLine(i); // Prints items      
}

我得到这个输出:

8
1
System.Collections.Generic.List`1[System.Object]

如何修改我的代码以查看该列表中的内容而不是收到此消息?avlDataList 列表有 10 个元素.所以我想总共看到 12 个元素.

How can I modify my code to see what's inside of that list instead of getting this message? That avlDataList list has 10 elements. So i would like to see 12 elements in total.

推荐答案

根据 MKasprzyk 的回答,您可以在遍历主列表时检查每个对象的类型,以决定如何将其输出到控制台.否则将调用默认的 ToString() 方法(见下文).

As per MKasprzyk's answer, you can check the type of each object when iterating through the main list to decide how to output it to the console. Otherwise the default ToString() method will be called (see below).

如果您使用的是 C# 7,您可以(可以说)通过使用 switch 模式匹配来提高可读性,只需编写:

If you are using C# 7, you can (arguably) increase readability by using switch pattern matching and simply write:

foreach (var i in mainList)
{
    switch (i)
    {
        case IEnumerable<object> li:
            Console.WriteLine($"List: {string.Join(", ", li)}");
            break;
        default:
            Console.WriteLine(i);
            break;
    }
}

这种方式,目前输出的是不可枚举的对象,而输出实现了IEnumerable接口的对象,比如List在一行中,逗号分隔,每个列表值使用对象的 ToString() 方法输出,例如:

This way, non-enumerable objects are output as at present, whereas objects that implement the IEnumerable<object> interface, such as a List<object> are output on a single line, comma separated, with each list value output using the object's ToString() method, e.g.:

23

45

列表:12, 54, 69, ...

List: 12, 54, 69, ...

为什么列表值显示为这样?

System.Collections.Generic.List`1[System.Object]

查看 文档List 类,ToString() 方法直接派生自 Object 基类,它只输出类型名称.这个类没有特定的覆盖来使它做一些特别的事情.

Looking at the documentation for the List<T> class, the ToString() method is derived directly from the base Object class, where it simply outputs the Type name. There is no specific override for this class to make it do something special.

您可以定义您自己的从 List 派生的泛型类,该类覆盖 ToString() 运算符以根据需要以特定方式显示项目,而不是在您的控制台输出代码中有特殊情况:

You could define your own generic class derived from List<T> that overrides the ToString() operator to show items in a particular way if you desired, rather than having special cases in your console output code:

public class ConsoleFriendlyList<T> : List<T>
{
    public override string ToString()
    {
        return $"List: {string.Join(", ", this)}";
    }
}

现在如果你修改你的代码来定义 avlDataList 如下,它会输出和上面一样的:

Now if you modified your code to define avlDataList as follows, it would output the same as above:

var avlDataList = new ConsoleFriendlyList<object>();

这篇关于列表中的列表不打印元素,而是显示 System.Collections.Generic.List`1[System.Object]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆