为什么我得到System.Collections.Generic.List`1 [System.String]而不是列表的内容? [英] Why am I getting System.Collections.Generic.List`1[System.String] instead of the list's contents?

查看:118
本文介绍了为什么我得到System.Collections.Generic.List`1 [System.String]而不是列表的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定制作一个小型的基于RPG的小型控制台来学习课程.我有一个基本的游戏,但是我有一个问题.我正在尝试显示另一个类中Player类的玩家清单.

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.

我正在使用列表保存球员清单.

I'm using a List to hold the players inventory.

public List<string> inventory = new List<string>();

这在Players类文件中.

This is in the Players class file.

然后在Shop Class中,我在做;

Then in the Shop Class, I'm doing;

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;

player.inventory.Add("Axe");

但是,如果我在控制台中打印出 player.inventory(); ,则会得到错误的结果:

But if I make the player.inventory(); print out in the console, I get the wrong result:

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

我该如何解决?我应该改用 Axe .

How can I fix this? I should get Axe instead.

推荐答案

这不是错误,它只是打印出列表.List的默认 ToString()实现只是打印类型的名称.

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.

我猜您希望它像在PHP中那样工作,在其中打印用逗号分隔的数组?试试:

I'm guessing you want it to work like in PHP, where you print arrays separated by commas? Try:

string.Join(", ", player.inventory);

这篇关于为什么我得到System.Collections.Generic.List`1 [System.String]而不是列表的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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