Is this a bug in record structs? #5541
Unanswered
dumptruckman
asked this question in
Q&A
Replies: 1 comment 1 reply
-
That example does have a primary constructor so the parameterless constructor is not synthesized. However, in .NET, all value types have an implicit default value and C# has always allowed |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I've stumbled upon something that seems like a bug with record structs. This proposal states:
And then gives some examples. The one in question is
However, you can still call
new R2();
and the compiler does not complain.Further, if this is in a nullable context, the result of this is that you can end up in a state where a non-nullable reference type is initialized as null.
You can fiddle around with an example of this here where I tried to hide the parameterless constructor to prevent using the automatically generated one. If you change that constructor to public then this compiles with a warning about converting a null literal to a non-nullable reference type. If you remove the extra constructor this will compile without warning which is definitely a problem since it hides the fact that you're getting a null value into a non-nullable reference type.
Beta Was this translation helpful? Give feedback.
All reactions