i have a question why it works like this
var a = new Tuple<object, object, object>(1, 1, 1);
var b = new Tuple<object, object, object>(1, 1, 1);
Console.WriteLine(a.Item1==b.Item1);//Output- False
Console.WriteLine(a.Equals(b));//Output- True
Console.WriteLine(a==b);//Output- False
Why so ?
i try to find some info about how it works, as far as i know this happens because of object type, and value type(int)
That's the different between value and reference types in C#.
Here you're comparing two
objectinstances. As for any reference type, the equality operator (unless overriden in that type) does reference comparison.When you store any value type (e.g.
intin your case) in the variable of reference-type (objectin your case), you're doing what's named boxing.This is because
Equalsmethod is overriden in theTupletype to check if its content are identical to another tuple. It is callingEqualsfor each of the items and returntrueif all of them are equal.Again, you have two instances of the
Tupleclass (any class is a reference type), and thatTupleclass doesn't have override for an==operator. Therefore, you're just checking if those two variables containing the same instance.