why make things more complex? why do this:
txtNumerator.Text =
txtNumerator.Text == "" ? "0" : txtNumerator.Text;
instead of this:
if txtNumerator.Text="" {txtNumerator.Text="0";}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Suppose you wanted to pass either zero or txtNumerator.Text to a method M. How would you do that?
You could say:
Or you could say
The latter is shorter and easier to read.
The larger point here is that statements are useful for their side effects and expressions are useful for their values. If what you want to do is control which of two side effects happens, then use an “if” statement. If what you want to do is control which value gets chosen from two possibilities, then consider using a conditional expression.
UPDATE:
Jenny asks why not just do this?
That’s fine if there’s just one condition to check. But what if there are, say, four? Now there are sixteen possibilities and writing the “if” statement for it gets messy to say the least:
Instead, you can just say: