Created
March 13, 2014 17:01
-
-
Save sdether/9532422 to your computer and use it in GitHub Desktop.
Easy immutable object derivation
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
public class Immutable { | |
public class Person { | |
public readonly string Firstname; | |
public readonly string Lastname; | |
public Person(string firstname, string lastname) { | |
Firstname = firstname; | |
Lastname = lastname; | |
} | |
public Person With(string firstname = null, string lastname = null) { | |
return new Person(firstname ?? this.Firstname, lastname ?? this.Lastname); | |
} | |
} | |
public void Example() { | |
var dad = new Person( | |
firstname: "Joe", | |
lastname: "Smith" | |
); | |
var son = dad.With(firstname: "Joe Jr."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interesting, but sad you have to.