Zelter Ady
Zelter Ady

Reputation: 6338

c# Strange result on compare operation

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?

Upvotes: 2

Views: 204

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1501646

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.)

Upvotes: 12

Related Questions