Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Strange result on compare operation

Tags:

c#

asp.net

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?

like image 487
Zelter Ady Avatar asked Nov 30 '25 02:11

Zelter Ady


1 Answers

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

like image 70
Jon Skeet Avatar answered Dec 02 '25 15:12

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!