mercredi 1 mars 2017

Condition check in async method

Question description: I am surprised that code inside the if block is reached. I supposed the if block won't be executed.

I am totally confused. Who can explain or teach me?

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
}

Aucun commentaire:

Enregistrer un commentaire