Brad
Brad

Reputation: 12255

What is this function I'm using? It works but why?

I found this article on the webs and thought I'd try to apply the null string style method to my excel range value. Sometimes it has a value in it and sometimes it doesn't and obviously double doesn't like null values.

This is my little snippet.

double valTotal = (rngTotal.Value != null ? 1 : 0);

My question is what am I doing with this above code? It looks like an if statement in one line with the "1" being the "then" and the "0" being the "else". Is that right? And most importantly, what is the name of this syntax so I can find out more about this?

Upvotes: 1

Views: 203

Answers (5)

Bill W
Bill W

Reputation: 1488

It is the conditional operator. Yes, it acts like an "inline if". It is a handy shortcut for short if expressions. The part before the '?' is a Boolean condition if the Boolean condition is true then the part after the '?' is evaluated otherwise the part after the ':' is evaluated. In other words the '?' and ':' act like the 'then' and 'else' in an if statement.

See the links below for more detail.

https://msdn.microsoft.com/en-us/library/ty67wk28.aspx

The ternary (conditional) operator in C

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500515

It's the conditional operator. (Sometimes incorrectly called the ternary operator; it's a ternary operator as it has three operands, but its name is the conditional operator. If another ternary operator is ever added to C#, "the ternary operator" will become an ambiguous/non-sensical phrase.)

Quick description:

The first operand is evaluated, and if the result is true, the second operand is evaluated and forms the overall value of the expression. Otherwise the third operand is evaluated and forms the overall value of the expression.

There's a little more detail to it in terms of type conversions etc, but that's the basic gist.

Importantly, the first operand is always evaluated exactly once, and only one of the second or third operand is evaluated. So for example, this is always safe:

string text = GetValueOrNull();
int length = text == null ? 5 : text.Length;

That can never throw a NullReferenceException as text.Length isn't evaluated if text == null is true.

It's also important that it's a single expression - that means you can use it in some places (such as variable initializers) where you couldn't use the if/else equivalent.

Closely related is the null-coalescing operator which is also worth knowing about.

Upvotes: 9

JaredPar
JaredPar

Reputation: 754715

This is known as the conditional / ternary operator in C#. It's essentially short hand for the following

double valTotal;
if (rngTotal.Value !=null) {
  valTotal = 1;
} else {
  valTotal = 0;
}

Upvotes: 1

Francesco Baruchelli
Francesco Baruchelli

Reputation: 7468

It's called conditional operator:

http://msdn.microsoft.com/en-us/library/ty67wk28(v=vs.80).aspx

Upvotes: 0

JohnD
JohnD

Reputation: 14757

It's the conditional operator:

http://msdn.microsoft.com/en-us/library/ty67wk28(v=vs.80).aspx

Upvotes: 2

Related Questions