Alex Rivera | Logout

Ternary ? operator vs the conventional If-else operator in c#

Asked 2012-07-25T05:12:58.393
45

Possible Duplicate:
Is the conditional operator slow?

I'm a massive user of the ? operator in C#. However my project manager frequently warns me that using ? operator might cost some performance compared to the If-Else statements in a large scale application. So I'm told to avoid using it. However, I love using it because it is concise and sort of keeps the code clean.

Is there such performance overhead when using ? operator?

Edit
Report

1 Answer

25

No.

Use what makes your code readable. If if statements do that, use them. If ternary operators do that, use them.

It is likely that both will compile down to the same IL anyway.

In any event the things that will slow down your application will likely be the database or the network or the hard drive ... anything except whether you used if statements or ternary expressions.

answered 2012-07-25T05:30:44.470

Your Answer