Reputation: 631
these are two same object but my condition is going into else block. how to compare two objects values same or not?
baObject b = new baObject();
baObject b1 = new baObject();
if (object.Equals(b, b1))
{
// Equal
}
else
{
// not equal
}
what about this kind of behaviour.
var t1 = repo.Model_Test_ViewAllBenefitCodes(2)
.OrderBy(p => p.ba_Object_id)
.ToArray();//.FirstOrDefault();
var t2 = x.ViewAllBenefitCodes
.OrderBy(p => p.ba_Object_id)
.ToArray();//.FirstOrDefault();
here t1 and t2 are same objects but getting from two different methods or functions.
Upvotes: 1
Views: 3578
Reputation: 10623
I doubt that they are the same objects. object.equals() would have returned true otherwise.
The default implementation of Equals supports reference equality for reference types, and bitwise equality for value types. Reference equality means the object references that are compared refer to the same object. Bitwise equality means the objects that are compared have the same binary representation.
Note that a derived type might override the Equals method to implement value equality. Value equality means the compared objects have the same value even though they have different binary representations. For example, consider two Decimal objects that represent the numbers 1.10 and 1.1000. The Decimal objects do not have bitwise equality because they have different binary representations to account for the different number of trailing zeroes. However, the objects have value equality because the numbers 1.10 and 1.1000 are considered equal for comparison purposes since the trailing zeroes are insignificant.
To achieve what you are explaining us, you are better left with overriding the Equals method, also to make use of the == and != operators we'll need to overload those.
Upvotes: 0
Reputation: 5479
You could probably override Equals() or == to compare the objects in whatever way you'd like (i.e. has the same ID property).
This way you control when two objects should be considered equal.
Upvotes: 0
Reputation: 31406
You're dealing with reference equality here by default. So with reference equality it's going to check if you actually have the same object - meaning that b and b1 are the same object in memory, not just equivalent, whatever that means for a baObject.
You can see near the bottom of that MSDN reference that you can define your own implementation of operator== so that you can do something like:
if (a1 == a2)
where a1 and a2 are objects of some type you've created. Whatever makes them equivalent is up to you - e.g., if they had certain properties that you could compare.
Back to reference equality - if you'd done something like this:
baObject b = new baObject();
baObject b1 = b;
if (object.Equals(b, b1))
it would evaluate as true because b and b1 are, in fact, the same object. But since that's probably not what you're after, you could define operator== for the baObject class and then compare them to see if they are equivalent. You could do something like:
public static bool operator ==(baObject b1, baObject b2) {
return b1.foo == b2.foo && b1.bar == b2.bar;
}
to define what equivalence means for your baObject.
Upvotes: 2