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

Casey Marshall cmarshall at pacificbiosciences.com
Tue Apr 8 14:51:13 EDT 2008


On Tue, 2008-04-08 at 19:26 +0100, Marek Safar wrote:
> Hello,
> > > >
> > > > 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?
> > > >
> > > This particular comparison is in-lined to false constant.
> > >
> >
> > Huh. Seems like an awfully bad way to handle that: if you aren't paying
> > attention, some subtle bugs would creep in to code assuming that '==
> > null' means something is uninitialized.
> >
> I am sorry but I did get this at all.
> 

I'm working with code that does this:

  if (valuetype != null)
  {
    // Code assuming `valuetype' is initialized.
  }

Which is wrong, since the test is (apparently) always true. Even worse
would be:

  if (valuetype == null)
  {
    // initialize `valuetype'
  }

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.

Or am I missing something?

Thanks.


More information about the Mono-devel-list mailing list