Skip to content

Instantly share code, notes, and snippets.

@maxkagamine
Created July 16, 2022 04:41
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 maxkagamine/cbfc1414b555df89030fb217dd277697 to your computer and use it in GitHub Desktop.
Save maxkagamine/cbfc1414b555df89030fb217dd277697 to your computer and use it in GitHub Desktop.
C# 9 Record Types: Automatic Modified Date
using System;
var foo = new Foo("foo");
var bar = foo with { Name = "bar" };
Console.WriteLine(foo);
Console.WriteLine(bar);
// Foo { CreatedOn = 7/15/2022 21:39:43, UpdatedOn = , Name = foo }
// Foo { CreatedOn = 7/15/2022 21:39:43, UpdatedOn = 7/15/2022 21:39:43, Name = bar }
abstract record Base
{
protected Base(Base original)
{
CreatedOn = original.CreatedOn;
UpdatedOn = DateTime.Now;
}
public DateTime CreatedOn { get; init; } = DateTime.Now;
public DateTime? UpdatedOn { get; init; }
}
record Foo(string Name) : Base;
@maxkagamine
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment