target-typed new
Writing new() without a type name where the type is already known from the left-hand side, as in Dictionary<string, decimal> prices = new();.
In more detail
It also works for fields, parameters and return values whose type is already declared. It removes repetition without hiding the type, since the declaration still names it.
Related terms
- var — The C# keyword that asks the compiler to infer a local variable's type from its initial value and then fix it there.
- collection expression — C# square-bracket syntax such as [1, 2, 3] or [] that builds whatever collection type the target declares.
Taught in
- Records, Pattern Matching, and Modern C# — Modern .NET Core, C# & Enterprise Microservices
- Manual Object Mapping — Modern .NET Core, C# & Enterprise Microservices