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

Comment Policy: Your words are your own, so be nice and helpful if you can. Limit the amount of links submitted in your comment. Do not respond here to other posts where the comments have been closed. Trust me, they have been closed for a reason.

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=""> <s> <strike> <strong>