The generated code in both cases is completely identical.
(You are missing brackets around the code in the first example, but I will just assume that it's a typo and you are actually asking about the difference betwen using return
and else
.)
If you look at the generated code for these two methods:
public static void Test1(int errorCode) {
if (errorCode > 0) {
Console.WriteLine(errorCode);
return;
}
Console.WriteLine("ok");
}
public static void Test2(int errorCode) {
if (errorCode > 0) {
Console.WriteLine(errorCode);
} else {
Console.WriteLine("ok");
}
}
It will look like this:
if (errorCode > 0) {
011A00DA in al,dx
011A00DB push eax
011A00DC mov dword ptr [ebp-4],ecx
011A00DF cmp dword ptr ds:[10F3178h],0
011A00E6 je 011A00ED
011A00E8 call 7470C310
011A00ED cmp dword ptr [ebp-4],0
011A00F1 jle 011A0100
Console.WriteLine(errorCode);
011A00F3 mov ecx,dword ptr [ebp-4]
011A00F6 call 73C5A920
return;
011A00FB nop
011A00FC mov esp,ebp
011A00FE pop ebp
011A00FF ret
}
Console.WriteLine("ok");
011A0100 mov ecx,dword ptr ds:[3E92190h]
011A0106 call 7359023C
}
011A010B nop
011A010C mov esp,ebp
011A010E pop ebp
011A010F ret
and:
if (errorCode > 0) {
011A0122 in al,dx
011A0123 push eax
011A0124 mov dword ptr [ebp-4],ecx
011A0127 cmp dword ptr ds:[10F3178h],0
011A012E je 011A0135
011A0130 call 7470C310
011A0135 cmp dword ptr [ebp-4],0
011A0139 jle 011A0148
Console.WriteLine(errorCode);
011A013B mov ecx,dword ptr [ebp-4]
011A013E call 73C5A920
011A0143 nop
011A0144 mov esp,ebp
011A0146 pop ebp
011A0147 ret
} else {
Console.WriteLine("ok");
011A0148 mov ecx,dword ptr ds:[3E92190h]
011A014E call 7359023C
}
}
011A0153 nop
011A0154 mov esp,ebp
011A0156 pop ebp
011A0157 ret
The generated code is completely identical, down to the last instruction.