Object Initializer

noun

say it: “OB-jekt ih-NISH-uh-ly-zer” /ˈɒbdʒɛkt ɪˈnɪʃəlaɪzər/

Curly braces after new that set an object's properties in the same statement that creates it.

What is Object Initializer in C#?

Object Initializer — Curly braces after new that set an object's properties in the same statement that creates it, instead of one assignment per line afterward.

Book book = new Book { Title = "Dune", Pages = 412 };
Console.WriteLine(book.Title); // Dune

class Book
{
    public string Title { get; set; } = "";
    public int Pages { get; set; }
}

In a sentence

  • “I created the book with an object initializer: new Book { Title = "Dune", Pages = 412 }.”

Level: Journeyman · More C# terms at this level

Updated