The C# 9.0 with keyword facilitates the creation of new records from existing records.
My question is: what technical reasons did or could motivate introducing this specific feature with a new keyword and syntax instead of just generating a new function with named parameters.
For example consider the example from the docs:
Person person1 = new("Nancy", "Davolio") { PhoneNumbers = new string[1] };
Person person2 = person1 with { FirstName = "John" };
Instead we could have had something like:
var person1 = new Person("Nancy", "Davolio", new string[1]);
var person2 = person1.With(FirstName:"John");
I assume that adding new syntax to a widely used language like C# based on careful consideration and that possibly it is documented somewhere.
It was originally proposed with a
Withmethod.In order to default parameter values from another object instance, there was a complicated new language feature proposed: caller-receiver parameters
The new design preserves the convenience of the old design but is much simpler for the compiler to implement.