[Mono-list] [Mono-dev] CS0019 for value type != null

Manuel de la Pena mandel at themacaque.com
Tue Apr 8 03:33:12 EDT 2008


I don't know if I have this correct, but until 2.0 value types were not
nullables. I have trid this solution my self but you could try this
alternative code:

Nullable<DateTime> dt = new Nullable<DateTime>(null);

or alternatively 

DateTime? dt = null;

and later do the check

if(dt != null)
    Console.WriteLine(dt + "");

I any way tried you code and the error code is CS0165(use of unassigned
local var) rather than CS0019 (Operator 'operator' cannot be applied to
operands of type 'type' and 'type'). IS the code you sent the exact code
that is giving you the problem?

Hope it helps,

On Mon, 2008-04-07 at 17:16 -0700, Casey Marshall wrote:
> I've noticed that on a recent SVN build of mono, I get a CS0019 error
> if
> I try to compare a value type against null. E.g.:
>   DateTime dt;
>   if (dt != null)
>     Console.WriteLine(dt);
> As far as I can tell, this is the correct behavior, since a value type
> can't be null. I'm concerned, though, because this behavior seems to
> have been introduced recently in SVN, AND it looks like MS's compiler
> doesn't reject this statement (well, I haven't seen it accept it, but
> this is existing code which apparently compiles fine under Visual
> Studio).
> So, my question is, what's going on here? Does anyone know what the
> semantics are if the compiler generates a conditional based on a
> `valuetype CMP null' test?
> Thanks.
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list

More information about the Mono-list mailing list