8

Possible Duplicate:
C# okay with comparing value types to null

Why does C# allow :

class MyClass
{
    public int MyInt;
}

static void Main(string[] args)
{
    MyClass m = new MyClass();
    if (m.MyInt == null) // <------------- ?
        Console.Write("......");
}

Resharper says "expression is always false" - which is obviously - true since MyInt is int and not int?

But how C# allow this to compile? The property will always be there and its type is int!

Community
  • 1
  • 1
Royi Namir
  • 144,742
  • 138
  • 468
  • 792

1 Answers1

-1

I think, this is simply for the same reason why would compile

if(false)
   Console.WriteLine("never get here");

Something that would never execute.

Worth mantioning that, yes you don't get error, but you get a warning on this.

Tigran
  • 61,654
  • 8
  • 86
  • 123