我找到了一个关于在类中使用 List 的代码示例。有些代码我不明白。Name 和 Description 字段在 List 定义中具有值,但 Album 字段没有值。(
new genre { Name = "Rock" , Description = "Rock music", Album?? }
)。为什么?
public class Genre
{
public string Name { get; set; }
public string Description { get; set; }
public List<Album> Albums { get; set; }
}
public class Album
{
public string Title { get; set; }
public decimal Price { get; set; }
public Genre Genre { get; set; }
}
var genre = new List<Genre>
{
new genre { Name = "Rock" , Description = "Rock music" },
new genre { Name = "Classic" , Description = "Middle ages music" }
};
new List<Album>
{
new Album { Title = "Queensrÿche", Price = 8.99M, Genre = genre.Single(g => g.Name == "Rock") },
new Album { Title = "Chopin", Price = 8.99M, Genre = genre.Single(g => g.Name == "Classic") }
};