[Mono-list] Differing nullable behavior

Robert Jordan robertj at gmx.net
Fri Aug 4 16:10:41 EDT 2006


Hey,

Pedro Sobota wrote:
> You are right. In this case, I don't get why csc lets the use of 'as' 
> with a nullable value type, if a conventional value type is not 
> supported. Should gmcs act like csc, or be coherent?

Please file a bug.

> Unrelated but there is also this curious case where:
> 
> static T CheckDBNull<T>(object value) {
>     if (value is DBNull) return (T)null; else return (T)value;
> }
> 
> is not allowed, but
> 
> static T CheckDBNull<T>(object value) {
>     return (T)((value is DBNull) ? null : value);
> }
> 
> is, on both compilers!

It's not curious at all:

 From the C# spec, (§20.1.1)

"The literal null cannot be converted to a type given by a type 
parameter, except if the type parameter is known to be a reference type 
(§‎20.7.4). However, a default value expression (§‎20.8.1) can be used 
instead. In addition, a value with a type given by a type parameter can 
be compared with null using == and != (§‎20.8.4) unless the type 
parameter has the value type constraint (§‎20.7.4)."


You might argue that the latter test case could fall
under this rule, but it doesn't because the ternary
expression (like every other complex expression)
hides the literal null.

Robert



More information about the Mono-list mailing list