1

我有以下代码将检查 aList<int>是否与 type 兼容IEnumerable<object>。协方差不适用于值类型,因此该表达式的计算结果应为 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);
    }
}

在上面的代码中,代码不会像预期的那样进入 if 块。但是,在监视窗口中,相同的表达式将计算为真。(见图。)

表达式在监视窗口中计算为真。

这是有原因的,还是 Visual Studio 中存在错误?(我使用的是 Visual Studio 2022 17.0.4)

4

0 回答 0