C#System.Collections.Generic.List`1 [System.String] [英] C# System.Collections.Generic.List`1[System.String]

查看:7570
本文介绍了C#System.Collections.Generic.List`1 [System.String]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在septemeber开始一个Web开发课程,我们要在C#中编程。所以我开始学习它,所以我有一些经验。现在我有约2年的HTML,CSS和PHP的经验,所以我立即选择C#。



我在学习课程,读。我决定做一个小的基于控制台的RPG来学习类。我有一个基本的游戏,但我有一个问题。我试图在另一个类中显示来自Player类的玩家库存。



我使用列表来保存玩家库存。

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

这是在玩家类文件中。



然后在商店类,我在做;

 玩家player = new Player 

创建一个新对象来访问player类。然后在商店的列表中添加一些东西,我在做;

  player.inventory.Add(Ax) ; 

但是如果我使 player.inventory(); 打印出来在控制台,我得到一个错误 - System.Collections.Generic.List`1 [System.String]。



有没有人可以帮助我可以吗?我需要提供更多的代码吗?

解决方案

这不是一个错误,它只是打印出来的列表。

我猜你想要的它像PHP一样工作,在那里你打印用逗号分隔的数组?尝试:

  string.Join(,,player.inventory); 


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.

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.

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

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

This is in the Players class file.

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");

But if I make the player.inventory(); print out in the console, I get an error - System.Collections.Generic.List`1[System.String].

Is there anyone that could help me please? Do I need to provide more code?

解决方案

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.

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

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

这篇关于C#System.Collections.Generic.List`1 [System.String]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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