C# 重写ToString,但打印数组时会显示类型
当我打印值时,我没有得到值,而是打印类型。 在C# 重写ToString,但打印数组时会显示类型,c#,C#,当我打印值时,我没有得到值,而是打印类型。 在PlayerBOclassConsole.WriteLine(playerList)中打印player[] 但我需要打印值 我的代码有什么问题 public class Program { public static void Main(string[] args) { Player[] p= new Player[100]; Console.WriteLine("Enter the number of
PlayerBO
classConsole.WriteLine(playerList)中代码>打印player[]
但我需要打印值
我的代码有什么问题
public class Program
{
public static void Main(string[] args)
{
Player[] p= new Player[100];
Console.WriteLine("Enter the number of players");
int n = int.Parse(Console.ReadLine());
int i;
for (i = 0; i < n; i++)
{
p[i] = new Player();
Console.WriteLine("Enter the player name");
p[i].Name = Console.ReadLine();
Console.WriteLine("Enter the country name");
p[i].Country = Console.ReadLine();
Console.WriteLine("Enter the skill");
p[i].Skill = Console.ReadLine();
}
PlayerBO pb=new PlayerBO();
pb.DisplayPlayerDetails(p);
}
}
public class Player
{
private string _country;
private string _skill;
private string _name;
public Player(string _name, string _country, string _skill)
{
this._name = _name;
this._country = _country;
this._skill = _skill;
}
public Player() { }
public string Name
{
get { return this._name; }
set { this._name = value; }
}
public string Country
{
get { return this._country; }
set { this._country = value; }
}
public string Skill
{
get { return this._skill; }
set { this._skill = value; }
}
public override string ToString()
{
return string.Format("{0,-20}{1,-20}{2,0}", Name, Country, Skill);
}
}
public class PlayerBO
{
public void DisplayPlayerDetails(Player[] playerList)
{
playerList = new Player[100];
Console.WriteLine("Player Details");
Console.WriteLine(playerList);
}
}
公共类程序
{
公共静态void Main(字符串[]args)
{
玩家[]p=新玩家[100];
Console.WriteLine(“输入玩家数量”);
int n=int.Parse(Console.ReadLine());
int i;
对于(i=0;i
控制台。WriteLine(playerList)
将执行为数组实现的ToString
,这与覆盖该数组中对象类型的ToString
不同
要打印数组中的值,需要对其进行迭代:
foreach(var item in playerList)
{
Console.WriteLine(item);
}
或者另一种方法是使用string.Join
:
Console.WriteLine(string.Join(Environment.NewLine, playerList));
另外,请查看汽车特性:
//Instead of this:
public string Name
{
get { return this._name; }
set { this._name = value; }
}
//You can do this:
public string Name { get; set; }
Console.WriteLine(playerList)
将执行为数组实现的ToString
,这与覆盖该数组中对象类型的ToString
不同
要打印数组中的值,需要对其进行迭代:
foreach(var item in playerList)
{
Console.WriteLine(item);
}
或者另一种方法是使用string.Join
:
Console.WriteLine(string.Join(Environment.NewLine, playerList));
另外,请查看汽车特性:
//Instead of this:
public string Name
{
get { return this._name; }
set { this._name = value; }
}
//You can do this:
public string Name { get; set; }
你不能这样做。您必须遍历数组才能打印值。虽然您在Player
项上覆盖了ToString
,但这不会影响ToString
在数组上的实现,这就是为什么您需要按照当前答案的建议执行并遍历数组的原因。旁注:以后请只发布相关内容部分代码来重现问题,而不是完整的代码转储,请参阅下面关于如何创建的内容。您不能这样做。您必须遍历数组才能打印值。虽然您在Player
项上覆盖了ToString
,但这不会影响ToString
在数组上的实现,这就是为什么您需要按照当前答案的建议执行并遍历数组的原因。旁注:以后请只发布相关内容部分代码要再现问题而不是完整的代码转储,请参阅下面有关如何创建的内容。旁注:虽然\n
很好,但我建议使用环境。换行符
仅用于良好实践。@TheLethalCoder-Yap:)同意您的意见。Corrected@Gayathri-太好了:)很高兴它帮助了您注意:虽然\n
很好,但我建议您使用环境。换行符
只是为了良好的实践。@TheLethalCoder-Yap:)同意您的看法。Corrected@Gayathri-太好了:)很高兴它帮助了你