Replies: 5 comments
-
I thinking you've overcomplicated the example: public struct S
{
public string Field { get; }
public S(string field) => Field = field;
} NRTs don't work very well with fields of structs, since you can default up a struct. |
Beta Was this translation helpful? Give feedback.
-
I guess it'll just have to remain an oddity until (if ever) we get non-zero default structs. |
Beta Was this translation helpful? Give feedback.
-
See #2328 for a potential solution |
Beta Was this translation helpful? Give feedback.
-
Still has a problem with arrays as their elements are set to default public struct S
{
public string Field { get; }
public S(string field) => Field = field;
}
static class C
{
static void Main()
{
var x = new S[10];
x[0].Field.Inspect(); // prints null
}
} |
Beta Was this translation helpful? Give feedback.
-
I agree. Any solution to non-zero default structs has to be a Core CLR change as Anything else is just kicking the can down the road. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Consider the following piece of code from Sharplab:
As can be seen from the link, despite me marking the struct as
T
not being able to benull
, it gives me no warning ondefault(S<string>)
and I end up with_field
(of typeT
) beingnull
. Therefore there seems no point in usingwhere T : notnull
with structs.Am I missing something?
Beta Was this translation helpful? Give feedback.
All reactions