Q:

Which is faster if-else or ? :

Which is faster in c# either
int a = 10;
bool _IsEven;
if(a%2 == 0)
{
   _IsEven = true;
}
else
{
   _IsEven = false;
}

Or
int a = 10;
bool _IsEven = a%2 == 0 ? true : false;

UPDATE
I know here I can optimize my code just writing 
bool _IsEven = a%2 == 0;

But my question is not about code optimization rather it is regarding about performance of these two statements???
Can you please help me to improve my coding knowledge?

A:

Well your second code wouldn't even compile. But you could just write:
bool isEven = a % 2 == 0;

or 
bool isEven = (a & 1) == 0;

Why use either the conditional operator or an if statement?
In general, when you have something like:
if (condition)
{
    x = true;
}
else
{
    x = false;
}

or
x = condition ? true : false;

you can just write:
x = condition;

I wouldn't be surprised to see the C# compiler optimize this anyway, but I wouldn't be as concerned with the performance as the readability.

A:

According to this benchmark comparison, if..else block would run just as fast as the ?..: ternary operation as fast as a single level switch/case statement.
So it would better, in my opinion, to go for a better readability and clearer statements than to worry about performance since it would be of minimal influence in your case.

A:

EDIT: If nothing else, read the conclusion at the bottom. Rest is trying to make case to the general reader why the conclusion makes sense.
Note that my immediate reaction to the question was, it shouldn't matter. And that, for most practical purposes, you should not find any difference... Anyone might even argue, that they are just two different ways of achieving the same thing... although readability may of course be different, depending on the actual three expressions involved in writing these statements. i.e.

Expression for the condition
Expression if the condition is true
Expression if the condition is false

And most importantly, as others have suggested, you should be able to figure this by (I assume release build)

Looking at generated IL.
Timing the code with large number of iterations.

But then assuming you are, for reasons good or bad, interested in micro optimizations, I also got curious and looked at it myself.. and purely for my amusement, this is what I found -
Note: The findings below must not be taken as absolute, because

They may depend on several factors unknown to me, including the 3 expressions mentioned above I chose to put in my test.
Even if all the factors were known, anyone would argue, that many of these are internal details of the C# compiler or the JIT compiler, and hence can change with any version of either compilers. My little curious experiment was on my machine.. .Net 4.5, Release build.

Experiment 1
Code
int a = 1;
int b = 2;
int h = 0;

h = a == b ? 10 : -10;
GC.KeepAlive(h);

IL for h = a == b ? 10 : -10;
IL_0006:  ldloc.0
IL_0007:  ldloc.1
IL_0008:  beq.s      IL_000e
IL_000a:  ldc.i4.s   -10
IL_000c:  br.s       IL_0010
IL_000e:  ldc.i4.s   10
IL_0010:  stloc.2

Assembly for             h = a == b ? 10 : -10;
00482B61  mov         eax,dword ptr [ebp-8]  
00482B64  cmp         eax,dword ptr [ebp-0Ch]  
00482B67  je          00482B73  
00482B69  nop  
00482B6A  mov         dword ptr [ebp-14h],0FFFFFFF6h  
00482B71  jmp         00482B7A  
00482B73  mov         dword ptr [ebp-14h],0Ah  
00482B7A  mov         eax,dword ptr [ebp-14h]  <-- Extra compared to 2nd experiment
00482B7D  mov         dword ptr [ebp-10h],eax  <-- Extra compared to 2nd experiment

Experiment 2
Code
int a = 1;
int b = 2;
int h = 0;

if (a == b) h = 10; else h = -10;
GC.KeepAlive(h);

IL for if (a == b) h = 10; else h = -10;
IL_0006:  ldloc.0
IL_0007:  ldloc.1
IL_0008:  bne.un.s   IL_000f
IL_000a:  ldc.i4.s   10
IL_000c:  stloc.2                              <-- Extra compared to 1st experiment
IL_000d:  br.s       IL_0012
IL_000f:  ldc.i4.s   -10
IL_0011:  stloc.2

Assembly for            if (a == b) h = 10; else h = -10;
0086288E  mov         eax,dword ptr [ebp-8]  
00862891  cmp         eax,dword ptr [ebp-0Ch]  
00862894  jne         008628A0  
00862896  mov         dword ptr [ebp-10h],0Ah  
0086289D  nop  
0086289E  jmp         008628A7  
008628A0  mov         dword ptr [ebp-10h],0FFFFFFF6h  

Analysis
I repeat, not being from C# or JIT compiler teams, this analysis is just my guess (I may have made errors, erroneous assumptions), and most likely, being implementation detail, could change in future versions. Last, but not the least, the optimizations may very well be driven by several other factors / variations not considered here at all.
Based on IL:
IL generated for Experiment 1 looks better, in the sense that while the end result is obviously the same, the compiler does seem to have generated 1 extra IL instruction for Experiment 2. i.e. each expression for if block and else block, is seemingly being treated independent, and hence, while you and I know that the code involved assignment to variable h, compiler treats the two code blocks as separate, and generate independent code for assignment to h if if..else.. is used.
IL_000c:  stloc.2                              <-- Extra compared to 1st experiment
....
IL_0011:  stloc.2

So using ?: operator, does seem to generate smaller IL, and hence potentially better code.. (Small Code can also be faster, because there are less instructions to load in execution cache of the CPU, and hence small, chance to execute faster based on other conditions.. However, do note that this is not machine code yet).
Later, Experiment 1 should be better than Experiment 2, gets proven to be incorrect, based on assembly code generated by JIT compiler. (Goes only to prove, that while IL optimizations has its place.. at the end, what matters is code generated by JIT compiler).
Based on Assembly (Counter Intuitive):
As it turns out, when the rubber hits the road, i.e. in assembly, Experiment 1 has two more instructions than Experiment 2, and should run slower (at micro level). Here is my guess why -
IL_0008:  beq.s      IL_000e
IL_000a:  ldc.i4.s   -10
IL_000c:  br.s       IL_0010
IL_000e:  ldc.i4.s   10
IL_0010:  stloc.2

00482B6A  mov         dword ptr [ebp-14h],0FFFFFFF6h   <-- IL_000a:  ldc.i4.s   -10

00482B73  mov         dword ptr [ebp-14h],0Ah          <-- IL_000e:  ldc.i4.s   10
00482B7A  mov         eax,dword ptr [ebp-14h]          <-- IL_0010:  stloc.2
00482B7D  mov         dword ptr [ebp-10h],eax          <-- IL_0010:  stloc.2

dword ptr [ebp-14h] apparently is a temporary variable, which stores the results of either Expression #2 (condition is true), or Expression #3 (condition is false) above.. (behavior may vary completely based on the expressions themselves.. I don't know).. and then one has to move the value from value from temporary variable into CPU register, and then from CPU register to the location allocated for local variable h on the stack.
IL_0008:  bne.un.s   IL_000f
IL_000a:  ldc.i4.s   10
IL_000c:  stloc.2
IL_000d:  br.s       IL_0012
IL_000f:  ldc.i4.s   -10
IL_0011:  stloc.2

00862896  mov         dword ptr [ebp-10h],0Ah          <-- IL_000a:  ldc.i4.s   10
                                                       <-- IL_000c:  stloc.2

008628A0  mov         dword ptr [ebp-10h],0FFFFFFF6h   <-- IL_000f:  ldc.i4.s   -10
                                                       <-- IL_0011:  stloc.2

Here, it is pretty clear, that the value 10 needs to be loaded on to a potentially temporary variable on the stack, and then immediately moved from that location, to location for variable h on the stack. I guess, by having the two load and store instructions right next to each other, it should be easy enough for JITter to figure that it could just store the value directly into the local variable, without the need for a temporary variable. And hence, the seemingly un-optimized IL code should perform better with the assembly code JITter generated.

Conclusion
While it was my personal choice to spend my time at night to write this long answer, Kudos to anyone who has read through this to reach this final conclusion of mine.

Micro Optimizations can vary not just by higher, language level constructs, but also various other factors (likely not in your control).. I am guessing, that the code in the 3 expressions would be as much important as your choice of if..else.. vs ?:, as well as the version of compiler and runtime used, as well as other conditions on the OS.

Hence, assuming micro optimization is super important to you, unless you are ready to run your performance test, for your very specific code (ignore the benchmark provided in another answer.. and in many cases, running your own tests for such micro optimizations can itself be pretty hard, because they can easily be influenced external, system wide factors like OS Scheduler etc), with each change you make in this code / each time runtime version changes (to check for performance regressions), the question you have is meaningless, and there is NO absolute answer to your question.

PS - I hope, this answer becomes a good example of these kind of (generic micro optimization / performance) questions on Stack Overflow, one come across almost every day :(.

