Visual C# Developer Center > Visual C# Forums > Visual C# Language > C# compiler generates a syntax error for a nullable variable using '?' operator but not using 'if' statement
Ask a questionAsk a question
 

AnswerC# compiler generates a syntax error for a nullable variable using '?' operator but not using 'if' statement

  • Wednesday, November 04, 2009 4:46 AMdjmarcus Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    How do I report this C# compiler bug to VS team:

    The compiler (VS2008 Pro) complains about the x = cond ? DateTime.Now : null; line in 'test2()'

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

    Note that if instead of '?" operator I use an 'if' statement, the compiler accepts the code.

    I thought the two forms are equivalent. So its either a VS bug or I'm missing something about the C# language.

    -TIA
    David


    The source code:

                //--
                private DateTime? test1(bool cond)
                {
                    Nullable<DateTime> x;

                    if (cond)
                        x = DateTime.Now;
                    else
                        x = null;

                    return x;
                }

                //--
                private DateTime? test2(bool cond)
                {
                    DateTime? x;

                    x = cond ? DateTime.Now : null;

                    return x;
                }

Answers

  • Wednesday, November 04, 2009 5:12 AMRon.Whittle Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    It's not a bug. What the compiler is complaining about is that DateTime and null aren't of the same type. For the a?b:c to work, b and c must be of the same type. It hasn't reached the assignment to x part of your code.

    a?b:c is not identical to if/then/else in this respect.


    Ron Whittle - If the post is helpful or answers your question, please mark it as such. Not As Brightly Lit
  • Wednesday, November 04, 2009 3:11 PMdjmarcus Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    My confusion came from the fact that I do not need to use type casting in 
    DateTime? x = DateTime.Now;

    Yet I have to use it in the '?' operator. I naively assumed that the compiler was smart enough to handle this as in this example:

    double y;
    y = cond ? 2.3D : 1;

    As the previous poster replied (Ron.Whittle), the real problem is that both operands of the '?' have to be of the same type (or implicitly convertible to the same type) for the compiler to do the assignment.

    This also explains, of course, why the following worked as well (the typecasting on the 'null').

    x = cond ? DateTime.Now : (DateTime?)null;

    Thanks folks for the clarification. I'm glad its not a VS bug.   :-)

    -David

All Replies

  • Wednesday, November 04, 2009 5:12 AMRon.Whittle Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    It's not a bug. What the compiler is complaining about is that DateTime and null aren't of the same type. For the a?b:c to work, b and c must be of the same type. It hasn't reached the assignment to x part of your code.

    a?b:c is not identical to if/then/else in this respect.


    Ron Whittle - If the post is helpful or answers your question, please mark it as such. Not As Brightly Lit
  • Wednesday, November 04, 2009 5:19 AMDeborahKMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed AnswerHas Code
    This worked for me:

     

     

    x = cond ? (DateTime?)DateTime.Now : null;
    



    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    • Proposed As Answer byDeborahKMVPWednesday, November 04, 2009 4:16 PM
    •  
  • Wednesday, November 04, 2009 3:11 PMdjmarcus Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    My confusion came from the fact that I do not need to use type casting in 
    DateTime? x = DateTime.Now;

    Yet I have to use it in the '?' operator. I naively assumed that the compiler was smart enough to handle this as in this example:

    double y;
    y = cond ? 2.3D : 1;

    As the previous poster replied (Ron.Whittle), the real problem is that both operands of the '?' have to be of the same type (or implicitly convertible to the same type) for the compiler to do the assignment.

    This also explains, of course, why the following worked as well (the typecasting on the 'null').

    x = cond ? DateTime.Now : (DateTime?)null;

    Thanks folks for the clarification. I'm glad its not a VS bug.   :-)

    -David