条件检查异步方法

时间:2017-03-01 09:51:47

标签: c# if-statement async-await

问题描述: 我很惊讶if块中的代码已到达。我认为if块不会被执行。

我完全糊涂了。谁能解释或教我?

private async Task TestMethod()
{
    bool ok = false;
    City city = City.BJ;
    await Task.Delay(100);
    ok = true;

    if (!ok)//Suppose it won't go into this code block. Because variable 'ok' is true.
    {
        if (city == City.BJ)//City is enum type.
        {
            throw new Exception("BJ. Good day.");
        }
        else
        {
            throw new Exception("SH. Rainy day");//This row is reached.Weird! But the upper caller method doesn't detect this exception.
        }
    }
}

enum City
{
    BJ = 1,
    SH = 2
}

0 个答案:

没有答案