A with
expression allows for "non-destructive mutation", designed to produce a copy of the receiver expression with modifications in assignments.
with
expression has a receiver with a non-void type.with
expression is a member_initializer_list
with a sequence of assignments to an identifier, which must be an accessible instance field or property of the receiver's type.Let's suppose you have a record with many properties, and you want to create a new record with only one or a few changes.
with
keyword and specify your changes on the right side.public class Program
{
public static void Main(string[] args)
{
Customer customer = new Customer
{
Name = "Mark",
Age = 34,
Address = "23 ashdown",
Country = "UK"
};
var newCustomer = customer with { Name = "John" };
Console.WriteLine(customer.Name);
Console.WriteLine(newCustomer.Name);
}
}
record Customer
{
public string Name { get; set; }
public int Age { get; set; }
public string Address { get; set; }
public string Country { get; set; }
}
As you can see in the above code, we created a customer record, and using the with
keyword another customer record is created only changing its name and all other properties remain as is.
customer
, and creates a new record newCustomer
with the Name
changed.with
expression causes the copy constructor to get called and then applies the object initializer on top to change the properties accordingly.