If you decompile this code, you will end up with:
public int Test()
{
throw new Exception();
}
I believe that since these are constant values, that the math is done at compile time, so 10/2 is not really 10/2, but 5...so it becomes trivial for the compiler to realize that 5==5 is always going to be true. In fact, I believe these constants will then automatically be translated into true. The compiler's goal is to optimize out code that is ALWAYS going to repeat and run the processing of it at compile time, rather than running the same processing over and over.
So, basically, the compiler realizes that since the if is always true
and the if results in a return
(via a throw
), so it optimizes out the code that it knows will never be executed. Thus, the decompiled code results in the above.
In fact, the opposite happens if you do the 10/2 == 6
, which is "constantized" into 5 == 6, which is turned into false. Since the if will always be false, it optimizes out the if:
public int Test()
{
int num = 0;
return num;
}