Single Line If Statement in C#

Here is a standard if … then statement in C# followed by a single line example. Using a single line if statement will reduce the number of lines of code.

if (dayOfTheWeek == "Tuesday")
{
    lunchLocation = "Fuddruckers";
}
else
{
    lunchLocation = "Food Court";
}

And the same example as a single line if … then statement.

lunchLocation = (dayOfTheWeek == "Tuesday") ? "Fuddruckers" : "Food Court";

Comments

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Current ye@r *