Le réseau pour les développeurs > Forums - Accueil > Visual C# Language > C# compiler generates a syntax error for a nullable variable using '?' operator but not using 'if' statement
Poser une questionPoser une question
 

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

  • mercredi 4 novembre 2009 04:46djmarcus Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    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;
                }

Réponses

  • mercredi 4 novembre 2009 05:12Ron.Whittle Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée
    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
  • mercredi 4 novembre 2009 15:11djmarcus Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée
    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

Toutes les réponses

  • mercredi 4 novembre 2009 05:12Ron.Whittle Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée
    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
  • mercredi 4 novembre 2009 05:19DeborahKMVPMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Réponse proposéeA du 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!
    • Proposé comme réponseDeborahKMVPmercredi 4 novembre 2009 16:16
    •  
  • mercredi 4 novembre 2009 15:11djmarcus Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée
    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