簡單的用法

當你需要建立物件並立即設定幾個屬性時,物件初始值設定項很方便,但可用的建構函式是不夠的。說你有課

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";