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

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

问题描述

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

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<object>接口的对象(例如List<object>)将以逗号分隔的一行输出,并且每个列表值都使用该对象的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]

查看文档> 类,ToString()方法是直接从基Object类派生的,它只输出Type名称.对于此类,没有特殊的替代方法可以使它做一些特别的事情.

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<T>的通用类,该类重写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天全站免登陆