This doesn't work because the compiler will not insert an implicit conversion on both sides at once, and null requires an implicit conversion to become a nullable type. Instead, you can write task. ActualEndDate = TextBoxActualEndDate.
Text! = ""? DateTime.
Parse(TextBoxActualEndDate. Text) : new DateTime?(); This only requires one implicit conversion (DateTime to DateTime? ).
Alternatively, you can cast either left side: task. ActualEndDate = TextBoxActualEndDate. Text!
= ""? (DateTime? )DateTime.
Parse(TextBoxActualEndDate. Text) : null; This also requires only one implicit conversion.
You really should use a DateTime. TryParse(TextBoxActualEndDate. Text, out someDateVar) there.
Never trust input to give you a parseable string. – Tomas Mar 15 '10 at 22:26 2 Yes, but it's not my code. – SLaks?
Mar 15 '10 at 22:26 2 Validation occurs in a couple places prior to this parse, and I would prefer an exception now rather than one when I attempt to insert DateTime. Min into the database. – Daniel Coffman Mar 15 '10 at 22:37.
The conditional operator doesn't look at what the value is being returned into. It only looks at the values it's being asked to choose between: a DateTime and null. It can't identify these as instances of the same type (because null isn't a valid DateTime), hence the error.
You and I know that Nullable could do the job, but the conditional operator isn't allowed to introduce "larger" types: it's only allowed to look at the types of the two expressions it's choosing between. (Thanks to Aaronaught in comments for clarification of this point and a nice clarifying example. ) To work around this, give the operator a hint by casting the DateTime: TextBoxActualEndDate.
Text! = ""? (DateTime?)(DateTime.
Parse(TextBoxActualEndDate. Text)) : null.
2 Mostly correct (+1): DateTime. Parse returns a DateTime (not a Nullable), which is a value type and has no conversion to or from null. The compiler doesn't have the ability to introduce "larger" types into the equation when it tries to resolve the expression, it can only work with the types that are actually there.It's the same reason you can't write Stream s = expr?
New MemoryStream() : new FileStream(...). – Aaronaught Mar 15 '10 at 22:29 Aaronaught: great explanation - I will fold that in. – itowlson Mar 15 '10 at 23:35.
This is a duplicate of stackoverflow.com/questions/858080/nulla... My answer to stackoverflow.com/questions/2215745/cond... gives an analysis that is germane to this question. I'll also be blogging about a similar issue with the conditional operator in April; watch the blog for details.
Blogs.msdn. Com/ericlippert/archive/2006/05/24/… was very informative. Thanks.
– Daniel Coffman Mar 16 '10 at 20:48.
The reason is that null is of type object so you have to cast it to the correct type, like this: task. ActualEndDate = TextBoxActualEndDate. Text!
= ""? DateTime. Parse(TextBoxActualEndDate.
Text) : ((DateTime? ) null).
The most correct way (IMO) is to do this task. ActualEndDate = TextBoxActualEndDate. Text!
= ""? (DateTime? )(DateTime.
Parse(TextBoxActualEndDate. Text) : null); I use the null collaescing operator frequently in this manner.
This is the error probably which you get in this situation: error CS0173: Type of conditional expression cannot be determined because there is no implicit conversion between '' and 'int') The compiler is is explaining that it does not know how convert null into a DateTime. Fix: you need to cast explicitly the expression which may return null to the nullable type. This will work ((DateTime?) null).
This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required. Nullable types and the ternary operator: why is `?
10 : null` forbidden? Conditional operator cannot cast implicitly? Conditional operator assignment with Nullable types?
Why can't I set a nullable int to null in a ternary if statement? Is there a difference between a ternary operator and an if statement in C#? How does cast in C#/.
Conditional operator assignment with Nullable types? Why is this code invalid in C#? Nullable type issue with?
Why do nullable bools not allow if(nullable) but do allow if(nullable == true)? How does the assignment of the null literal to a System. Nullable type get handled by .
Why doesn't this C# code compile? Nullable types and the ternary operator: why is `? 10 : null` forbidden?
Boxing / Unboxing Nullable Types - Why this implementation? Why does the conditional operator always return an int in C#? Why use Nullable when the type can be assigned Null already?
Weird behaviour with conditional operator in . Issue with setting a DateTime? Why am I allowed to compare a non-nullable type with null?
Why this Conditional operator error? Why && and || cannot be used for nullable type? Is it possible to assign a value to a nullable using the conditional operator?
Why does the == operator work for Nullable when == is not defined?
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.