Shortform C# statements (ternary operations)

By | May 14, 2014
int crap = 5;
int nocrap = Int32.TryParse(textBox1.Text, out nocrap) ? nocrap : 0;
crap = crap >= nocrap ? crap : nocrap;
MessageBox.Show(crap.ToString());

…aka if nocrap is greater than crap, then make crap=nocrap, else leave crap alone.

or without textbox input:

int crap = 5;
int nocrap = 32 ? nocrap : 0; //modify nocrap value to be larger
                  //or smaller than crap to see difference
crap = crap >= nocrap ? crap : nocrap;
MessageBox.Show(crap.ToString());

Leave a Reply

Your email address will not be published. Required fields are marked *