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";
    lunchLocation = "Food Court";

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

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

Liked this post? Follow this blog to get more. Follow


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 *