我找到了一個關於在類內使用List的代碼示例。有些代碼我不明白。名稱和說明字段在列表定義中有值,但是相冊字段沒有值。(列表<T>在類裏面使用
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 =「搖滾音樂」;'沒有'genre.Albums =任何'。 – David 2013-03-27 13:46:36