Conditional Operator

Today I learned about the C# syntax for representing conditionals.

t ? x : y – if test t evaluates to true, then evaluate and return x; otherwise, evaluate and return y.

As an example of this in practice;

string name = (nameVariable == null) ? string.Empty : nameVariable;

is the same as saying the following

string name;
if (nameVariable == null)
    name = string.Empty;
else
    name = nameVariable;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.