object initializer
C# syntax that sets properties in braces right after new, such as new Product { Name = "Kettle", Price = 24.50m }.
In more detail
It runs after the constructor and is the only place init-only properties can be set outside the constructor. Collection initialisers do the same for adding items.
Related terms
- init accessor — A property accessor written init instead of set, which allows the value to be assigned only while the object is being created.
- required modifier — The C# required keyword on a property, which makes the compiler refuse any construction that does not set it.
- collection expression — C# square-bracket syntax such as [1, 2, 3] or [] that builds whatever collection type the target declares.
Taught in
- The C# Language: Types, Variables, and Flow Control — Modern .NET Core, C# & Enterprise Microservices
- Classes, Interfaces, and Object-Oriented C# — Modern .NET Core, C# & Enterprise Microservices
- Collections, Generics, and LINQ — Modern .NET Core, C# & Enterprise Microservices