我做了这个Object Player(int number, string name, string guild, int hp, int mana)
并从 CSV 文件中读取玩家列表。
public static List<Player> GetPlayerFromCSV(string file)
{
List<Player> result = new List<Player>();
using (StreamReader sr = new StreamReader(file))
{
string line = sr.ReadLine();
if (line != null) line = sr.ReadLine();
while (line != null)
{
Player pl = AddPlayer(line);
if (pl != null) result.Add(pl);
line = sr.ReadLine();
}
}
return result;
}
private static Player AddPlayer(string line)
{
line = line.TrimEnd(new char[] { ';' });
string[] parts = line.Split(new char[] { ';' });
if (parts.Length != 5) return null;
Player pl = new Player(parts[0], parts[1], parts[2], parts[3], parts[4]);
return pl;
}
public override string ToString()
{
return Number + " - " + Name;
}
它给了我一个错误,因为第 0、3 和 4 部分应该是字符串,因为它是一个字符串数组。我已经尝试过parts[0].ToString()
等,但它没有帮助