x


Whats the result of comparing two objects of a user defined type using the equals operator?

I have this code where I compare two objects of a simple Card class I created. They reside on a List:

List<Card> aCards = new List<Card>(rows * cols);

The problem is that when I compare two items of that list with the equals operator they return true, but it was supposed to return false, because this operation is reference based and I'm pretty sure the objects in this list are deferents; they were created through a loop, meaning separated scopes. In this case:

bool test1 = aCards[0] == aCards[5];
bool test2 = System.Object.ReferenceEquals(aCards[0], aCards[5]);

The value of test1 is true; test2 is false. As I said it's a simple class, I didn't override any base method. Maybe the Mono default implementation is deferent, I don't know. Hoping for a clarification.

Thanks.

more ▼

asked Jan 14 '11 at 05:25 AM

dehwizard gravatar image

dehwizard
33 3 3 9

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I found the problem. My Card class was inheriting from UnityEngine.Object; I believe this class overloads the equal operator to act deferent, maybe value based. As soon as I turn it off (and the Card class inherited from the default System.Object) everything works as expected.

more ▼

answered Jan 14 '11 at 07:47 AM

dehwizard gravatar image

dehwizard
33 3 3 9

How did you force it to use System.Object?

Dec 21 '12 at 11:36 PM MacSecrest
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x180
x39
x15

asked: Jan 14 '11 at 05:25 AM

Seen: 1010 times

Last Updated: Dec 21 '12 at 11:36 PM