2

I have a strange behavior on my c# code (asp.net code behind). I post here a screenshot of the watch window:

strange null comparation result

I have an object user that is null (as you can see) and the compare: user == null returns false. I don't understand why!

Is someone here that can explain why this happens?

Zelter Ady
  • 6,266
  • 12
  • 48
  • 75

1 Answers1

12

Well you can easily write your own class which would give that result:

public class Evil
{
    public static bool operator ==(Evil lhs, Evil rhs)
    {
        return false;
    }

    public static bool operator !=(Evil lhs, Evil rhs)
    {
        return false;
    }
}

We're only guessing, at the moment... but that explains the symptoms.

(I've just tried overriding ToString, and I can't get the desired output.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @Daniel: It's an example of what could show the given output. – Jon Skeet Oct 02 '12 at 17:46
  • 1
    I think `return null` is more likely in the `ToString()` implementation. `return "null"` would cause the quotes to be displayed in the debugger. – Douglas Oct 02 '12 at 17:46
  • It's just speculation as to why such a comparison would return null in one case, but false in the null check. – Jason Larke Oct 02 '12 at 17:47
  • It have been long time i used Wathc in VS but the string would be `"null"`? – Damian Leszczyński - Vash Oct 02 '12 at 17:48
  • Curious, how does the static Object.Equals(object, object) behave in this case? Could it be used to test for the presence of Evil? – Aaron Anodide Oct 02 '12 at 17:49
  • @AaronAnodide Perhaps `Object.ReferenceEquals` as the `Evil` object could still override the `Equals` implementation. EDIT: Sorry, misread. Not sure if `Object.Equals` calls the virtual `Equals` method or not on the object. – Chris Sinclair Oct 02 '12 at 17:50
  • @AaronAnodide: `object.Equals(((Evil)null), null)` behaves correctly with this `Evil` class. It has no actual instance to call `.Equals()` on, so it returns true after determining that both objects are null. (It could still cause incorrect behavior if you compared `object.Equals(new Evil(), null)` if you override the `.Equals()` method in the Evil class, though. – StriplingWarrior Oct 02 '12 at 17:55
  • @StriplingWarriorm why shouldnt be, typicaly you compare reference if they are falce then you go for equal(). Probably if operator for == would return always true then the object.Equal(object,object) will fail. – Damian Leszczyński - Vash Oct 02 '12 at 17:58
  • @StriplingWarrior, What is the product of `typeOf((Evil)null)` ? – Damian Leszczyński - Vash Oct 02 '12 at 18:02
  • Thanks for your answer. Someone in my team did an override definition for the == operator and i didn't think to check it and there was the error. You helped me a lot! – Zelter Ady Oct 02 '12 at 18:05
  • 1
    @Vash: I don't think `typeof((Evil)null)` is valid code; you need to provide a compile-time type reference, not an object instance. Maybe you're thinking of `((Evil)null).GetType()`? (which would throw a `NullReferenceException` of course) – Chris Sinclair Oct 02 '12 at 18:25