简单的用法
当你需要创建对象并立即设置几个属性时,对象初始值设定项很方便,但可用的构造函数是不够的。说你有课
public class Book
{
public string Title { get; set; }
public string Author { get; set; }
// the rest of class definition
}
要使用初始化程序初始化类的新实例:
Book theBook = new Book { Title = "Don Quixote", Author = "Miguel de Cervantes" };
这相当于
Book theBook = new Book();
theBook.Title = "Don Quixote";
theBook.Author = "Miguel de Cervantes";