Reputation: 576
For single line if statement I have always preferred to use curly braces myself. Like
if (condition)
{
statement;
}
This statement can be written as
if(condition)
statement;
or
if(condition) statement;
I have searched through SO and other forum, most of the people prefer the first option. I know there's no effect in the run time performance. Is there any performance factor for the first approach in compile time. How .NET framework or CLR handle decision statements like if or if-elseif ? Do the next options (second and the third) have any kind of enhanced performance ? May be the difference is negligible but I want to know how compiler handle this situation.
Upvotes: 0
Views: 263
Reputation: 2438
It doesn't.
C# compiled into CIL (Common Intermediate Language) which is the "assembly language" for the CLR (similar to "bytecode" in the Java world).
In CIL there is no concept of "braces". If-Else code blocks are expressed in CIL using "branch" commands, which are like conditional jumps. For example, "brc" followed by name of a label will jump to the label if the latest values on the stack are equal.
So, it doesn't matter if you put braces or not - it will be expressed the same way in the generated CIL code.
Upvotes: 2