假设我有一个 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
};