Last active
January 24, 2020 12:59
-
-
Save bitbonk/323263bdacd5e495b0e04573b28c264b to your computer and use it in GitHub Desktop.
Record and discriminated unions in C# 9
This file contains 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 Person | |
{ | |
public initonly string Firstname { get; } | |
public initonly string Lastname { get; } | |
}; | |
enum class ByteOrBool { byte Y; bool B;} | |
enum class MixedType | |
{ | |
Person P; | |
ByteOrBool U; | |
} | |
var bart = new Person() | |
{ | |
Firstname = ”Bart”; | |
Lastname = “Simpson”; | |
}; | |
var lisa = bart with { Firstname = "Lisa" }; | |
var unionRecord = new MixedType.P(bart); | |
var unionType1 = new MixedType.U(B true); | |
var unionType2 = new MixedType.U(Y 0x42); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good question. It seems that primary constructors are still part of the overarching records proposal but it looks like there are quite some issues that need to be worked out.
There is also a wild discussion about it here: dotnet/csharplang#2691