It all depends on how expensive the expression is successful.
It should also be noted that the two versions are not semantically equivalent, since evaluating an if-expression can have side effects 1 .
If you really run into performance issues, then measure, don't guess . Measurement will be the only way to see what the actual performance is.
1 , , -:
CreateProcess , true:
bool CreateProcess(string filename, out handle) { ... }
if (CreateProcess("program.exe", out handle))
{
if (someCondition)
{
handle.SomeMethod(...);
}
if (someOtherCondition)
{
handle.SomeOtherMethod(...);
}
}
:
if (CreateProcess("program.exe", out handle) && someCondition)
{
handle.SomeMethod(...);
}
if (CreateProcess("program.exe", out handle) && someOtherCondition)
{
handle.SomeOtherMethod(...);
}