Skip to content

Instantly share code, notes, and snippets.

@mvcds
Last active June 5, 2020 13:18
Show Gist options
  • Save mvcds/a2b6af39a1f10f0a30ba1910a91ec47c to your computer and use it in GitHub Desktop.
Save mvcds/a2b6af39a1f10f0a30ba1910a91ec47c to your computer and use it in GitHub Desktop.
c#-record
//the data modifier transforms the class into a record
//the constructor and destructor are not relevant for the example but make it easier
public data class Person
{
string FirstName;
string LastName;
public Person(string firstName, string lastName)
=> (FirstName, LastName) = (firstName, lastName);
public void Deconstruct(out string firstName, out string lastName)
=> (firstName, lastName) = (FirstName, LastName);
}
var person = new Person("Han", "Solo");
//the with expression gets a "patch function" which
//creates a new Person based on the old one but with the differences set on the expression
var otherPerson = person with { LastName = "Groupie" };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment