Nullable types and the ternary operator. Why won't this work?

And null is, well, null. There's no implicit conversion between those two hence the error message. If you change the right-hand expression to one of the following then it compiles because there is an implicit conversion between int?

And null (#1) and between int and int?(#2).GetBoolValue()?(int?)10 : null // #1 GetBoolValue()?10 : (int? )null // #2.

Hmm.. never knew that. Thanks! – BFree May 13 '09 at 14:05 1 You can also write new int?().

– SLaks? Apr 9 '10 at 16:02.

10 : (int?)null; Basically what is happening is that conditional operator is unable to determine the "return type" of the expression. Since the compiler implictitly decides that 10 is an int it then decides that the return type of this expression shall be an int as well. Since an int cannot be null (the third operand of the conditional operator) it complains.By casting the null to a Nullable we are telling the compiler explicitly that the return type of this expression shall be a Nullable.

You could have just as easily casted the 10 to int? As well and had the same effect.

Incidentally, the Microsoft implementation of the C# compiler actually gets the type analysis of the conditional operator wrong in a very subtle and interesting (to me) way. Here's my article on it: blogs.msdn.com/ericlippert/archive/2006/....

10 : (int?)null; The reason you see this is because behind the scenes you're using Nullable and you need to tell C# that your "null" is a null instance of Nullable.

Doh! Andrew got their first. +1 to him.

– Martin Peck May 13 '09 at 13:57.

Just add an explict cast. Int? X = GetBoolValue()?10 : (int?)null; It is the ternary operator that gets confused - the second argument is an integer and so is the third argument exspected to be an integer, too, and null does not fit.

The problem is that the ternary operator is inferring type based on your first parameter assignment...10 in this case, which is an int, not a nullable int. You might have better luck with: int? X = GetBoolValue() (int?)10 : null.

It's because the compiler determines the type of the conditional operator by it's second and third operand, not by what you assign the result to. There is no direct cast between an integer and an null reference that the compiler can use to determine the type.

Basically what is happening is that conditional operator is unable to determine the "return type" of the expression. Since the compiler implictitly decides that 10 is an int it then decides that the return type of this expression shall be an int as well. Since an int cannot be null (the third operand of the conditional operator) it complains.

By casting the null to a Nullable we are telling the compiler explicitly that the return type of this expression shall be a Nullable. You could have just as easily casted the 10 to int? As well and had the same effect.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions