jeudi 11 octobre 2018

Does assigning a variable in an if statement impact performance?

Does assigning variables in an if statement impact performance?

e.g.:

private IntPtr _handle;

public void Open()
{
    //_handle = new IntPtr(1);

    if ((_handle = new IntPtr(1)) == null)
    {
        throw new Exception("wtf");
    }
}

I saw that if the variable is assigned in the if statement then it generates the code as:

public void Open()
{
    IntPtr intPtr = _handle = new IntPtr(1);

    bool flag = (intPtr == null);

    if (flag)
    {
        throw new Exception("wtf");
    }
}

but if not, if it was written like the commented out part in the example and just (_handle == null), then the generated code doesn't have the IntPtr intPtr = _handle = new IntPtr(1), instead it just uses the _handle variable. So, does the extra assignment impact performance?

Note: I got the generated code via ILSpy.

Aucun commentaire:

Enregistrer un commentaire