I have the following code which will check if a List<int>
is compatible with type IEnumerable<object>
. Covariance does not apply to value types, so this expression should evaluate to false.
public class WatchWindowTests
{
[Fact]
public void WhenRun_WillEvalueToFalseInCode_AndTrueInWatchWindow()
{
object listOfInts = new List<int>();
var x = false;
// This line evaluates to false - covariance does not apply to value types
if(listOfInts is IEnumerable<object>)
{
// This line is never executed
x = true;
}
Assert.False(x);
}
}
In the code above, the code will not enter the if block, as expected. However, in the watch window, the same expression will evaluate to true. (See image.)
Is there a reason for this, or is there a bug in Visual Studio? (I'm using Visual Studio 2022 17.0.4)