Type result with Ternary operator in C#

Posted by Vaccano on Stack Overflow See other posts from Stack Overflow or by Vaccano
Published on 2010-05-11T23:01:43Z Indexed on 2010/05/11 23:04 UTC
Read the original article Hit count: 259

I am trying to use the ternary operator, but I am getting hung up on the type it thinks the result should be.

Below is an example that I have contrived to show the issue I am having:

class Program
{
    public static void OutputDateTime(DateTime? datetime)
    {
        Console.WriteLine(datetime);
    }

    public static bool IsDateTimeHappy(DateTime datetime)
    {
        if (DateTime.Compare(datetime, DateTime.Parse("1/1")) == 0)
            return true;

        return false;
    }

    static void Main(string[] args)
    {
        DateTime myDateTime = DateTime.Now;
        OutputDateTime(IsDateTimeHappy(myDateTime) ? null : myDateTime);
        Console.ReadLine();                        ^
    }                                              |
}                                                  |
// This line has the compile issue  ---------------+

On the line indicated above, I get the following compile error:

Type of conditional expression cannot be determined because there is no implicit conversion between '< null >' and 'System.DateTime'

I am confused because the parameter is a nullable type (DateTime?). Why does it need to convert at all? If it is null then use that, if it is a date time then use that.

I was under the impression that:

condition ? first_expression : second_expression;

was the same as:

if (condition)
   first_expression;
else
   second_expression;

Clearly this is not the case. What is the reasoning behind this?

(NOTE: I know that if I make "myDateTime" a nullable DateTime then it will work. But why does it need it?

As I stated earlier this is a contrived example. In my real example "myDateTime" is a data mapped value that cannot be made nullable.)

© Stack Overflow or respective owner

Related posts about c#

Related posts about ternary-operator