Skip to content

Instantly share code, notes, and snippets.

@jonschoning
Created November 13, 2020 01:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonschoning/3fbad75bee079cf7f864158d08494413 to your computer and use it in GitHub Desktop.
Save jonschoning/3fbad75bee079cf7f864158d08494413 to your computer and use it in GitHub Desktop.
using System;
static void Run()
{
Employee foo = new Employee("Aysha", "Stein", "1234");
Person bar = (Person)foo with { FirstName = "John", LastName = "Doe"};
Console.WriteLine($"foo: {foo}");
Console.WriteLine($"bar: {bar}");
// Expected Output:
// 1) foo: Employee { FirstName = Aysha, LastName = Stein, EmployeeNumber = 1234 }
// bar: Employee { FirstName = John, LastName = Doe, EmployeeNumber = 1234 }
// 2) foo: Employee { FirstName = John, LastName = Doe, EmployeeNumber = 1234 }
// bar: Employee { FirstName = John, LastName = Doe, EmployeeNumber = 1234 }
// 3) foo: Employee { FirstName = Aysha, LastName = Stein, EmployeeNumber = 1234 }
// bar: Person { FirstName = John, LastName = Doe }
// 4) foo: Employee { FirstName = John, LastName = Doe, EmployeeNumber = 1234 }
// bar: Person { FirstName = John, LastName = Doe }
}
Run();
public record Person(string FirstName, string LastName);
public record Employee(string FirstName, string LastName, string EmployeeNumber) : Person(FirstName, LastName);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment