A piece of C# code
var isTrue = (new List<int>{1,2,3} is IEnumerable<object>);
I got result false
in code execution,
but when I copy that code into WATCH window, the result is true
.
To bring up the Run and Debug view, select the Run and Debug icon in the Activity Bar on the side of VS Code. You can also use the keyboard shortcut Ctrl+Shift+D. The Run and Debug view displays all information related to running and debugging and has a top bar with debugging commands and configuration settings.
This is not a complete answer (I don't know the reasons why this bug is cropping up), but it sheds some light on the erratic behaviour of the debugger which is obviously buggy.
First and foremost: C# disallows (and AFAIK, the CLR too) type variance involvig value types; variance is only allowed if there is an identity preserving conversion between the involved types, otherwise it will fail (there is no identity preserving conversion for value types):
object[] oo = new int[] {1, 2, 3}; //will fail
IEnumerable<object> oo = new int[] {1, 2, 3}; //will fail
The debugger's immediate window is obviously wrong, new List<int> { 1, 2, 3 } is IEnumerable<object>
should return false
as the runtime does. Why is it returning true
? Because there's a bug, period.
What makes it even more bewildering is the fact that new int[] { 1, 2, 3 } is IEnumerable<object>
will correclty return false
when int[]
is implicitly convertible to IEnumerable<int>
same as List<int>
.
The only reason I find for the latter correct behavior is that the compiler already flags that expression as always false
with a warning and therefore the way the compiler analyzes the array scenario is different from any other IEnumerable
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With