-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implicit operator throws when checking ArraySegment != null or ArraySegment == null #8949
Comments
ArraySegment is a struct so can never actually be null; do you want to check if its array is null?
|
D'oh! This just started throwing out of the depths of an older test when we moved to .net core 2.0. Fair 'nuf that the answer is we have bad test code that was previously sneaking through. I know the implicit operator docs say it should not throw so I wanted to make sure to follow up. |
I do wonder what it will do for struct tests in generics e.g.
Where T is ArraySegment |
The generic tests you suggested work fine when T is an ArraySegment. Perhaps worth noting that (although it's rather pointless) we can compare other value types == or != null without an exception. |
I think we are talking here about breaking the contract on null check on struct types. It doesn't really matter whether it makes sense or not to do a null check. Null check on struct compiles but throws an undescriptive NullRef which breaks the former behavior. We need to fix this. |
It's a .Net Core runtime issue. Cool. https://github.com/dotnet/coreclr/issues/14012
Thanks @stephentoub and prioritizing it. Redirected from https://github.com/dotnet/corefx/issues/26987. |
We should add a test to corefx (if there is not one already). |
I'll do so. |
Symptom
Using != or == operator to compare an ArraySegment to null throws an ArgumentNullException. For example:
if (new ArraySegment<byte>() == null)
Cause
The != and == operator trigger implicit conversion from an array to ArraySegment on
null
. The resulting call to the ArraySegment constructor with a null array parameter throws an ArgumentNullException:public static implicit operator ArraySegment<T>(T[] array) => new ArraySegment<T>(array);
results in:
new ArraySegment<T>(null);
which throws ArgumentNullException.
dotnet/corefx#10528
dotnet/coreclr#9926
The text was updated successfully, but these errors were encountered: