5

I was just wondering why the following code doesn't work (please keep in mind that I set age to be nullable):

myEmployee.age = conditionMet ? someNumber : null;

Yet the following works fine:

if(conditionMet)
{
    myEmployee.age = someNumber;
}
else
{
    myEmployee.age = null;
}

Why can't I set the value to null in a conditional operator?? All these if statements in my code is not nice.

Thanks.

PaulG
  • 6,920
  • 12
  • 54
  • 98
  • 3
    Please define _code doesn't work_. – jrummell Apr 23 '12 at 15:15
  • possible duplicate of [Conditional operator assignment with Nullable types?](http://stackoverflow.com/questions/75746/conditional-operator-assignment-with-nullablevalue-types) – Jon Skeet Apr 23 '12 at 15:16
  • 1
    please note - it's not an 'inline if'; it's the *conditional operator*. `if` statements are statements (i.e. without value); the conditional operator `?:` is an expression (and hence why you've had this issue as the types must be the same or implicitly convertible). – Andras Zoltan Apr 23 '12 at 15:16
  • My apologies, guess I used the wrong keywords when searching and hadn't realized that this has already been asked. – PaulG Apr 23 '12 at 15:23

4 Answers4

19

The types on both sides have to be the same (or be implicitly convertible):

myEmployee.age = conditionMet ? someNumber : (int?)null;

From the docs:

Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • +1, in this case the ternary operator's rules for *true-expr* and *false-expr* play a role in it as well. – user7116 Apr 23 '12 at 15:16
  • Any reason you went after the `null` instead of casting as `(int?)someNumber`? Do you find that more readable? I only ask because both you and RedFilter chose the same side. – Marc Apr 23 '12 at 15:18
  • Both are possible - personally I find it easier to read this way, but it's just personal preference. – BrokenGlass Apr 23 '12 at 15:19
  • @Marc: I would choose that because `int` is logically convertible to a nullable integer but `null` is not logically convertible to an integer. – user7116 Apr 23 '12 at 15:22
8

You can, just be clear about the type that null should be interpreted as:

myEmployee.age = conditionMet ? someNumber : (int?)null; 
D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
2

Look at what the documentation has to say:

[MSDN] The default value for a nullable type variable sets HasValue to false. The Value is undefined.

The default is thus null. Which means that you can simply do:

if (conditionMet)
    myEmployee.age = someNumber;

There is no need for more obscure code syntax.


If required you can initialize it with null in advance, if it somehow was not the default, like so:

myEmployee.age = null;

if (conditionMet)
    myEmployee.age = someNumber;
Tamara Wijsman
  • 12,198
  • 8
  • 53
  • 82
0

The types of the parts of the ternary statement must be implicitly castable to a common base. In the case of int and null, they aren't. You can solve this by making age a Nullable<int> (or int?) and casting someNumber to int?.

EDIT to clarify: you can set the value to null with a ternary statement with proper casting. The problem here isn't that you're trying to set a value to null with a ternary statement. It's that the compiler-inferred return types of the two expressions involved in the ternary statement cannot be implicitly casted to a common base type.

Tom Hamming
  • 10,577
  • 11
  • 71
  • 145