当我在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方法为异步时才会出现此问题。