Re: Casting null to nullable
Curt Hagenlocher <[email protected]>
| Newsgroups | gmane.comp.windows.devel.dotnet.clr |
|---|---|
| Message-ID | <[email protected]> |
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 DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com