...

/

Projecting Sequence into New Types

Projecting Sequence into New Types

Learn about object initialization syntax and how to efficiently use object initialization in LINQ projections.

Object initialization

Before we look at projection, we need to review object initialization syntax. If we have a class defined, then we can instantiate an object using the class name, new(), and curly braces to set initial values for fields and properties, as shown in the following code:

Press + to interact
// Person.cs
public class Person
{
public string Name { get; set; }
public DateTime DateOfBirth { get; set; }
}
// Program.cs
Person knownTypeObject = new()
{
Name = "Boris Johnson",
DateOfBirth = new(year: 1964, month: 6, day: 19)
};

Compiler inference

Although we did not specify a type, the compiler can infer ...