Last active
June 5, 2020 13:18
-
-
Save mvcds/a2b6af39a1f10f0a30ba1910a91ec47c to your computer and use it in GitHub Desktop.
c#-record
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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