How to Answer: C# 9.0 - Provide an example of using a 'with' expression
Advice and answer examples written specifically for a C# Advanced job interview.
2. C# 9.0 - Provide an example of using a 'with' expression
This interview question shows the developer's understanding of C# expressions.
The 'with' expression allows you to copy a record type to another instance, keeping the unchanged values the same, but allowing you to create a new instance and changing only the values you wish to change. This offers convenience in not having to redefine the variable values
Animal animal1 = new("Feline", "Aspthicus");
var animal2 = animal1 with {Specie = "Baculatus" };
Console.WriteLine(animal1.Genus +" "+ animal1.Specie);// output Feline Aspthicus
Console.WriteLine(animal2.Genus+" "+animal2.Specie); // output Feline Baculatus
Written by Edward Danganan on April 21st, 2021