I have a checkbox that depending on the checked state I will perform different operations.
I know one option is avaiable;
If (chk.Checked)
{
}
else
{
}
But I also know I can use something smilar to;
if chk.Checked(true) ? DoJob1 : DoJob2
The problem is that I can't get this second option to work. Can somebody please explain to me where and when the second example could be used?
Loading
AlanPosted Nov 12, 2007, 8:50 AM
The ternary operator ?: produces an expression and, while some types of expressions can be used as statements, this is not one of them.
If the two methods produce the same (or compatible) return types, other than void, then you could do this:
int result = chk.Checked ? DoJob1() : DoJob2();
but, generally, this sort of stuff doesn't aid readability and I'd stick to if/else if I were you.