C# System.Collections.Generic.List`1[System.String]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16106181/
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
C# System.Collections.Generic.List`1[System.String]
提问by Jon
So I'm starting a Web Development course in septemeber, and we're gonna be programming in C#. So I started to learn it so I have some experience in it. Now I have ~2 years experience in HTML, CSS and PHP, so I picked C# up straight away.
所以我将在 9 月开始一门 Web 开发课程,我们将使用 C# 进行编程。所以我开始学习它,所以我在这方面有一些经验。现在我在 HTML、CSS 和 PHP 方面有大约 2 年的经验,所以我立即选择了 C#。
I'm learning classes, and I learn by doing, not reading. I decided to make a small little console based RPG to learn classes. I have a basic game going, but I have a problem. I'm trying to display the players inventory from the Player class in another class.
我正在学习课程,我在实践中学习,而不是阅读。我决定制作一个基于控制台的小型 RPG 来学习课程。我有一个基本的游戏,但我有一个问题。我试图在另一个类中显示 Player 类中的玩家库存。
I'm using a List to hold the players inventory.
我正在使用列表来保存玩家库存。
public List<string> inventory = new List<string>();
This is in the Players class file.
这是在 Players 类文件中。
Then in the Shop Class, I'm doing;
然后在 Shop Class 中,我正在做;
Player player = new Player();
To make a new object to access the player class. Then to add something to the List in the shop, I'm doing;
制作一个新的对象来访问播放器类。然后在商店的List中添加一些东西,我正在做;
player.inventory.Add("Axe");
But if I make the player.inventory();print out in the console, I get an error - System.Collections.Generic.List`1[System.String].
但是如果我player.inventory();在控制台中打印出来,我会得到一个错误 - System.Collections.Generic.List`1[System.String]。
Is there anyone that could help me please? Do I need to provide more code?
有没有人可以帮助我?我需要提供更多代码吗?
回答by Gopesh Sharma
You require a foreachloop to iterate each and every member of your list. You can't just print the list as a whole.
您需要一个foreach循环来迭代列表中的每个成员。您不能只打印整个列表。
You need something like:
你需要这样的东西:
foreach(var i in Player.Inventory)
{
Console.WriteLine(i);
}
回答by DGibbs
You're trying to print a list object and not the values contained within the list, you need something like: -
您正在尝试打印列表对象而不是列表中包含的值,您需要如下内容:-
foreach(var i in player.Inventory)
{
Console.WriteLine(i);
}
Edit* As per comments
编辑* 根据评论
Console.Write(string.Join(System.Environment.NewLine, player.Inventory));
回答by Ilya Ivanov
print next string to console, if you want to print all items at once:
打印下一个字符串到控制台,如果你想一次打印所有项目:
string items = string.Join(Environment.NewLine, player.inventory);
Console.WriteLine(items);
this will print each item in separate line
这将在单独的行中打印每个项目
By default ToString()(which is called on list when you pass it to Console.WriteLine) will print the typeof the list, not the contents itself. That's why you receive System.Collections.Generic.List`1[System.String].on console screen.
默认情况下ToString()(当您将它传递给时在列表上调用Console.WriteLine)将打印列表的类型,而不是内容本身。这就是您收到System.Collections.Generic.List`1[System.String] 的原因。在控制台屏幕上。
回答by Artless
It's not an error, it's just printing out the list. The default ToString()implementation of List simply prints the name of the type.
这不是错误,只是打印出列表。ToString()List的默认实现只是打印类型的名称。
I'm guessing you want it to work like in PHP, where you print arrays separated by commas? Try:
我猜您希望它像在 PHP 中一样工作,在其中打印以逗号分隔的数组?尝试:
string.Join(", ", player.inventory);
回答by skyfree
If you want to input all the inventory items, the flowing code should work:
如果你想输入所有的库存项目,流动的代码应该工作:
foreach (string inventoryItem in player.inventory)
{
Console.WriteLine(inventoryItem);
}
the player.inventory is a collection object and cannot be input directly.
player.inventory 是一个集合对象,不能直接输入。

