【发布时间】:2019-06-11 04:01:53
【问题描述】:
List<int> MyList = new List<int>();
MyList.Add(1);
MyList.Add(2);
MyList.Add(3);
MyList.Add(4);
foreach(int item in MyList){
System.Console.WriteLine(MyList);
}
这是我的代码显示的内容:
System.Collections.Generic.List`1[System.Int32]
System.Collections.Generic.List`1[System.Int32]
System.Collections.Generic.List`1[System.Int32]
System.Collections.Generic.List`1[System.Int32]
【问题讨论】:
-
只打印号码:
System.Console.WriteLine(item); -
您对列表中的每个“项目”都有一个循环,因此您需要处理项目,而不是“我的列表”。打印项目而不是 'MyList'
-
您正在使用 foreach 显示列表对象,但您尝试编写列表对象.. 更改 System.Console.WriteLine(MyList);到 System.Console.WriteLine(item);
标签: c#