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.

4
  • 3
    Please define code doesn't work. Commented Apr 23, 2012 at 15:15
  • possible duplicate of Conditional operator assignment with Nullable<value> types? Commented Apr 23, 2012 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). Commented Apr 23, 2012 at 15:16
  • My apologies, guess I used the wrong keywords when searching and hadn't realized that this has already been asked. Commented Apr 23, 2012 at 15:23

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

+1, in this case the ternary operator's rules for true-expr and false-expr play a role in it as well.
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.
Both are possible - personally I find it easier to read this way, but it's just personal preference.
@Marc: I would choose that because int is logically convertible to a nullable integer but null is not logically convertible to an integer.
8

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

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

Comments

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;

Comments

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.

1 Comment

You missed the last line in the question I think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.