假设我有一Album
堂课:
public class Album
{
public string Name {get; set;}
public string Artist {get; set;}
public int Year {get; set;}
public Album()
{ }
public Album(string name, string artist, int year)
{
this.Name = name;
this.Artist = artist;
this.Year = year;
}
}
当我想将数据分配给类型的对象时Album
,接下来的两种方法有什么区别:
通过构造函数
var albumData = new Album("Albumius", "Artistus", 2013);
或实例化时
var albumData = new Album
{
Name = "Albumius",
Artist = "Artistus",
Year = 2013
};