【发布时间】:2013-03-27 13:42:31
【问题描述】:
我找到了一个关于在类中使用 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") }
};
【问题讨论】:
-
哪一部分你不明白?
Name和Description值已设置,而Albums未设置。它与genre.Name = "Rock"; genre.Description = "Rock Music";相同,而不是genre.Albums = whatever。
标签: c# list class generic-list