Re: Casting null to nullable
Brady Kelly <[email protected]>
| Newsgroups | gmane.comp.windows.devel.dotnet.clr |
|---|---|
| Message-ID | <[email protected]> |
I came to the same conclusion this morning, while taking my walk, and have just confirmed this. > -----Original Message----- > From: Discussion of development on the .NET platform using any managed > language [mailto:[email protected]] On Behalf Of Curt > Hagenlocher > Sent: 15 November 2007 05:26 PM > To: [email protected] > Subject: Re: [DOTNET-CLR] Casting null to nullable > > On 11/15/07, Rich Armstrong <[email protected]> wrote: > > > > I'm aware of one situation that requires an explicit cast: > > > > // This produces an error "Type of conditional expression > > // cannot be determined because there is no implicit conversion > > // between 'int' and '<null>'" > > int? nullableInt = someCondition ? 0 : null; > > > > // This compiles without error > > int? nullableInt = someCondition ? 0 : (int?)null; > > > I'll bet you can also say > int? nullableInt = someCondition ? (int?)0 : null > > The compiler has to be able to find a common type between "0" and > "null" in > order to generate code, and apparently it doesn't in this case. The > fact > that the variable declaration is "int?" is a bit of a red herring as > the > type of the lhs of the expression is not taken into account when > evaluating > the rhs. > > -- > Curt Hagenlocher > [email protected] > > =================================== > This list is hosted by DevelopMentorR http://www.develop.com > > View archives and manage your subscription(s) at > http://discuss.develop.com =================================== This list is hosted by DevelopMentorĀ® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com