Why false != false?

Viewed 155

I have a method that check if 2 arguments are equal:

using System;

class Program{
    static void Main(string[] args){
        CheckEquality(false, false);
        Console.ReadKey();
    }
    public static void CheckEquality(object a, object b)
    {
        if (a == b){
            Console.WriteLine("yes");
        }else{
            Console.WriteLine("no");
        }
    }
}

As you can see, 2 arguments are "false", but when I run the program, the console says:

no
1 Answers

You're actually not comparing values here.

You are comparing references. In this case references to object containing bool values.

So while the values inside the objects might be the same, the equals (==) operator for the object only compares the references, not the values. Since you have two distinct boxes the references are different, hence the result is false.

Related