default(ImmutableArray<T>)
boxing
#115670
Replies: 2 comments 3 replies
-
ImmutableArray<int> ia = default;
IEnumerable e = ia;
bool b1 = ia == null; // produces true
bool b2 = e == null; // produces false If this can't be changed anymore to make more sense, ImmutableArray<T> will always be remembered as the "a bit how ya goin" type... |
Beta Was this translation helpful? Give feedback.
-
An uninitialized It’s not “actually” boxing to null (as @elgonzo shows), but boxing to a legitimate heap object that, through C# code, throws It’s an illusion. And it breaks when you poke at it. The null comparison is a lie. One that makes an uninitialized struct act like an unallocated class. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently, this throws an exception:
I'm not sure if this can be changed at this point, but I was thinking about what the correct behavior would be in a perfect world, and I had an idea that
e
would becomenull
above. This would allow non-aware receivers ofIEnumerable
to do a simple null check instead of the reflection helper above.That raises the question, how do we determine which structs become
null
when boxed? I immediately rememberedNullable<T>
and how it's special cased for this purpose. Could we potentially come up with a generalized approach?If the above is not feasible for any reason, can we add a special non-generic interface?
PS. It's also possible this is a bug (.NET 8) and it's supposed to act as an empty collection.
Beta Was this translation helpful? Give feedback.
All reactions