我正在寻找初始化另一个对象中列出的对象的更好且正确的方法?例如,假设我有两个类Player
和Team
,而Team
类中的一个属性是Player
的通用列表,如
public List<Player> Players{ get; set; }
如何在初始化Player
的内部初始化Team
?我的意思是,假设我Player
没有任何对象,并且想在创建/初始化Team
类对象时创建它们
void Main()
{
}
public class Player
{
public string Name { get; set; } = string.Empty;
public Player(){
Name = string.Empty;
}
}
public class Team
{
public string Name { get; set; }
public List<Player> Players{ get; set; }
public Team(){
Name = string.Empty;
Players = new List<Player>();
}
}
答案 0 :(得分:2)
你只是这个意思吗?
public Team(){
Name = string.Empty;
Players = new List<Player> {
new Player(), new Player() //etc
};
}
如果您有每个球员的姓名列表,可以将其传递给Team构造函数(或创建重载),如下所示:
public Team(IEnumerable<string> playerNames){
Name = string.Empty;
Players = new List<Player>();
foreach(var player in playerNames {
Players.Add(new Player { Name = player});
};
}
答案 1 :(得分:2)
您可以在构造函数中初始化变量:
public class Team
{
public string Name { get; set; }
public List<Player> Players { get; set; }
public Team()
{
Name = string.Empty;
Players = new List<Player>()
{
new Player() { Name = "A"},
new Player() { Name = "B"},
new Player() { Name = "C"}
};
}
}
或者您可以使用C#6.0中称为自动属性初始化程序的语言功能:
public class Team
{
public string Name { get; set; } = string.Empty;
public List<Player> Players { get; set; } = new List<Player>()
{
new Player() { Name = "A"},
new Player() { Name = "B"},
new Player() { Name = "C"}
};
}
区别仅在于语法。两种方式都会导致执行相同的程序。
答案 2 :(得分:1)
根据您拥有的代码,实例化的Team
的玩家列表为空。要将玩家添加到该列表中,只需将实例化的Player
添加到列表中,就像对任何List<T>
一样。
var myTeam = new Team();
var myPlayer = new Player();
myTeam.Players.Add(myPlayer);
如果您想在Team
的初始化过程中执行此操作,则可以采用相同的方法:
public class Team
{
// ...
public Team()
{
Players = new List<Player>();
this.Players.Add(new Player());
this.Players.Add(new Player());
}
}
您也可以一行完成此操作:
public class Team
{
// ...
public Team()
{
Players = new List<Player>()
{
new Player(),
new Player()
};
}
}
想必您想通过构造函数参数接收这些玩家吗?如果是这样,则添加构造函数参数,而不是直接实例化新播放器:
public class Team
{
// ...
public Team(IEnumerable<Player> players)
{
Players = new List<Player>(players);
}
}
请注意,此代码创建了一个新列表(其中具有相同的对象),而不是使用传递到构造函数中的集合对象。
答案 3 :(得分:0)
如果要在初始化团队时初始化玩家,则可以将玩家列表作为团队构造函数的参数,如下所示:
public Team(List<Player> players)
{
Players = players;
}
然后以这种方式调用:
var team = new Team(new List<Player>() { new Player("name1"), new Player("name2") });
对于构造函数参数,您最好采用类型更多的抽象类型,例如IEnumerable
,这将允许您在初始化时传递其他集合:
public Team(IEnumerable<Player> players)
{
Players = players;
}
最终代码如下:
class Program
{
static void Main(string[] args)
{
var team = new Team("Juventus",
new List<Player>
{
new Player("Ronaldo"),
new Player("Messi")
});
}
}
public class Player
{
public string Name { get; set; }
public Player(string name)
{
Name = name;
}
}
public class Team
{
public string Name { get; set; }
public IEnumerable<Player> Players { get; set; }
public Team(string name, IEnumerable<Player> players)
{
Name = name;
Players = players;
}
}
答案 4 :(得分:0)
如果您想让每个Team ojbect中都已经初始化过的球员的默认列表,则可以使用以下语法:
public class Team
{
public string Name { get; set; } = string.Empty;
public IEnumerable<Player> Players { get; set; } = new List<Player>();
}