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
C# compiler generates a syntax error for a nullable variable using '?' operator but not using 'if' statement
- 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.-TIADavidThe source code://--private DateTime? test1(bool cond){Nullable<DateTime> x;if (cond)x = DateTime.Now;elsex = null;return x;}//--private DateTime? test2(bool cond){DateTime? x;x = cond ? DateTime.Now : null;return x;}
Answers
- 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- Marked As Answer byHarry ZhuMSFT, ModeratorTuesday, November 10, 2009 4:54 AM
- Proposed As Answer byDeborahKMVPWednesday, November 04, 2009 4:16 PM
- My confusion came from the fact that I do not need to use type casting inDateTime? 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
- Marked As Answer byHarry ZhuMSFT, ModeratorTuesday, November 10, 2009 4:54 AM
All Replies
- 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- Marked As Answer byHarry ZhuMSFT, ModeratorTuesday, November 10, 2009 4:54 AM
- Proposed As Answer byDeborahKMVPWednesday, November 04, 2009 4:16 PM
- 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
- My confusion came from the fact that I do not need to use type casting inDateTime? 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
- Marked As Answer byHarry ZhuMSFT, ModeratorTuesday, November 10, 2009 4:54 AM


