IIF equivalent in C#

by Emanuele 6/17/2008 3:32:00 PM
How many times did you write this code?

if (a == 1)
{
    a = 1;
}
else
{
    a = a - 1;
}

I say too much.
In Vb or in VB.net there is an operator called iif that do this in one row.
Is there IIF equivalent in C#?
Yes!!!

a==1 ? a=1 : a=a - 1;

You can use the operator ?: to obtain this goal.
The syntax is very simple:

<bool condition> ? <true value> : <false value>;


Enjoy!

Related posts



Powered by BlogEngine.NET 1.3.1.0
Theme by Emanuele Bartolesi

About the author

Name of author Emanuele Bartolesi
I'm a senior developer and project manager.

Contact me Contact me

Calendar

<<  December 2008  >>
MoTuWeThFrSaSu
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in

Download Day 2008