1

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?

Brad
  • 11,934
  • 4
  • 45
  • 73
  • 1
    Although it's different from the "?" operator, You might also be interested in reading about the ?? operator: http://msdn.microsoft.com/en-us/library/ms173224.aspx – JohnD Aug 30 '11 at 16:28

5 Answers5

9

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • +1 for explaining why it's sometimes mistakenly called the ternary operator. – mcanterb Aug 30 '11 at 16:28
  • And yet, searching for "ternary operator" on Google is so much easier than searching for "?: operator" – cobbal Aug 30 '11 at 16:36
  • In the Null-coalescing operator link the example code has a line ` int? x`. Does this allow x to both be nullable and an integer value? if I do this when I define my variable would I not have to use the conditional operator. i.e. could I replace `double valTotal = (rngTotal.Value != null ? 1 : 0);` with `double? valTotal` and then it would be allowed to be null as a `double`? – Brad Aug 30 '11 at 16:40
  • 1
    @cobbal: But that's not the dichotomy - it's between "conditional operator" and "ternary operator". – Jon Skeet Aug 30 '11 at 16:41
  • @Brad: Nullable value types are a whole other topic... just search for "nullable value types" and you should get a bunch of hits. – Jon Skeet Aug 30 '11 at 16:41
  • @JonSkeet I was under the impression that it didn't have a name in other languages (such as C), but I appear to be incorrect. – cobbal Aug 30 '11 at 16:46
  • In C and C# it has always been _the_ (only) ternary operator. Anticipating on another one is very cautious. – H H Aug 30 '11 at 16:52
  • 1
    @Henk: Even without an extra one, it's still a bad idea to call it the ternary operator, as *all* that tells you is the number of operands - nothing about its *functionality* which is surely what the name should be about. It's not like we call the binary operators "binary operator 1, binary operator 2, binary operator 3" etc - no, we name them after their functionality. – Jon Skeet Aug 30 '11 at 17:00
2

It's the conditional operator:

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

JohnD
  • 14,327
  • 4
  • 40
  • 53
1

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;
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
1

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

Community
  • 1
  • 1
Bill W
  • 1,428
  • 1
  • 20
  • 29
0

It's called conditional operator:

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

Francesco Baruchelli
  • 7,320
  • 2
  • 32
  • 40