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

Marek Safar marek.safar at seznam.cz
Tue Apr 8 15:38:42 EDT 2008


Hello,
> I'm working with code that does this:
>
>   if (valuetype != null)
>   {
>     // Code assuming `valuetype' is initialized.
>   }
>   
This is correct, and `valuetype' will be always initialized

> Which is wrong, since the test is (apparently) always true. Even worse
> would be:
>
>   if (valuetype == null)
>   {
>     // initialize `valuetype'
>   }
>   
This check is wrong because in this scenario valuetype is never equal to 
null.

> meaning `valuetype' would never be initialized. It's a little nit-picky,
> since I don't think this results in any real bugs, since the value types
> here are usually simple (e.g., Rectangle, DateTime), but it easily
> could. It almost seems like the buggy gmcs was doing a good service -- I
> don't understand why == and != can be used with null vs. a value type,
> if they always evaluate to false and true.
>   
The rules are quite complicated here, but in this case both null and 
valuetype can be lifted.

Marek


More information about the Mono-devel-list mailing list