Created
July 16, 2022 04:41
-
-
Save maxkagamine/cbfc1414b555df89030fb217dd277697 to your computer and use it in GitHub Desktop.
C# 9 Record Types: Automatic Modified Date
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
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://sharplab.io/#gist:cbfc1414b555df89030fb217dd277697