Condition false, but code inside the if statement executed

前端 未结 3 1864
醉梦人生
醉梦人生 2020-12-19 22:55

I have the following method:

public bool ConnectAsync()
{
    if (IsConnected)
        throw new InvalidOperationException(\"Socket is already connected\");
         


        
3条回答
  •  一生所求
    2020-12-19 23:27

    This answer explained the problem I was having: https://stackoverflow.com/a/27552124/1830461

    Something of this format compiled to 64-bit can cause the debugger to step into the if statement:

            if (falseCondition) 
                throw new Exception(); 
            try { }  catch { }
    

    It's a bug in Visual Studio. The code isn't actually executed. The solution is to just ignore it and continue stepping through the code. Putting a lock statement on it fixes it, because it is no longer in that format.

提交回复
热议问题