dotnet-daily-tips

054 - with expression

When I did some F# programming back in the day I really like the with keyword that allowed to create a copy of an object with one or more updated fields.

Thanks to that you didn’t have to manually copy over the ones that didn’t change. F# had immutability baked in for a long time so it’s not a surprise that such concepts existed.

When immutability came to C#, it also had to adopt similar patterns to avoid over re-assigning unchanged properties.

Just recently I reminded myself that there is this option, that with record types, we can construct a new one with some properties updated.

Underneath, a Clone method is called with setting the property but this is more concise, and I think more readable.

Docs 📑: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/with-expression

var aNewHopeMovie = new Movie("A New Hope", 1977);
var fullMovieTitle = aNewHopeMovie with { Title = "SAtar Wars: A New Hope"};

// Will print: Movie { Title = Star Wars: A New Hope, ReleaseYear = 1977 }
Console.WriteLine(fullMovieTitle);

record Movie(string Title, int ReleaseYear);