Visual Studio调试器在if语句中对.Any()的行为不正确

时间:2017-04-06 11:18:04

标签: c# visual-studio

当我在Visual Studio 2015中调试此代码时,调试器会进入if语句。这是为什么?代码似乎没有执行。

private static async Task Test()
{
    var somethingasync = await Task.FromResult(0);

    var list = Enumerable.Empty<object>();

    if (list.Any())
        ;   // This line is reached by the debugger (but is not executed)
}

如果我在if之后添加了其他代码,则无法访问该行。

private static async Task Test()
{
    var list = Enumerable.Empty<object>();

    if (list.Any())
        ;   // This line is not reached

    var foo = "bar";
}

如果我将list.Any()分配给变量并检查变量,则该行未到达:

private static async Task Test()
{
    var list = Enumerable.Empty<object>();

    var hasElements = list.Any();

    if (hasElements)
        ;   // Does not reach this line

    if (list.Any())
        ;   // This line is reached    
}

仅当then方法为异步时才会出现此问题。

0 个答案:

没有答案