Comparing Array Objects

  • Thread starter Thread starter Raphael Iloh
  • Start date Start date
R

Raphael Iloh

Hi all, I'm having problems comparing array objects. Take a look at this:

int[] array1 = new int[]{1};
int[] array2 = new int[]{1};
Console.Writeln(array1.Equals(array2));

One would expect the above expression to return true as both arrays are
identically the same but it keeps returning false. Any info on how to solve
this problem will be appreciated.
 
No the objects are not equal. Equals implements reference equality not value
equality. You are essentially asking whether or not object 1 and object 2
live at the same address. They don't. They possibly live across the street
from each other but not in the same house. What you are searching for is
value equality. This method can be overridden by a derived class.
 
....thanks Alvin but your explanation falls short given the following
example:

DateTime date1 = new DateTime(2004, 02, 24);
DateTime date2 = new DateTime(2004, 02, 24);
Console.WriteLine(date1.Equals(date2));

DateTime is a value object and date1 and date2 were instantiated
exclusively, yet the outcome is always true.The same applies to integers,
booleans, etc; all value objects. Rather the problem I'd raised earlier on
is regarding Array objects specifically and we both know Array objects are
reference objects.

Regards, Raphael.


Alvin Bruney said:
No the objects are not equal. Equals implements reference equality not value
equality. You are essentially asking whether or not object 1 and object 2
live at the same address. They don't. They possibly live across the street
from each other but not in the same house. What you are searching for is
value equality. This method can be overridden by a derived class.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
Raphael Iloh said:
Hi all, I'm having problems comparing array objects. Take a look at this:

int[] array1 = new int[]{1};
int[] array2 = new int[]{1};
Console.Writeln(array1.Equals(array2));

One would expect the above expression to return true as both arrays are
identically the same but it keeps returning false. Any info on how to solve
this problem will be appreciated.



--
Regards,
Raphael Iloh (www.ilohraphael.net)
MCP, MCAD (C#)
This Posting is provided "AS IS", with no warranties and confers no rights.
 
Raphael Iloh said:
...thanks Alvin but your explanation falls short given the following
example:

DateTime date1 = new DateTime(2004, 02, 24);
DateTime date2 = new DateTime(2004, 02, 24);
Console.WriteLine(date1.Equals(date2));

DateTime is a value object and date1 and date2 were instantiated
exclusively, yet the outcome is always true.The same applies to integers,
booleans, etc; all value objects. Rather the problem I'd raised earlier on
is regarding Array objects specifically and we both know Array objects are
reference objects.

I don't think there's much benefit in bringing value types into the
equation here - they have equality tests in a slightly different way
(IIRC, something examines the fields with reflection).

The real reason is that Array doesn't override the Equals method
(unlike, say, String).
 
The real reason is that Array doesn't override the Equals method
(unlike, say, String).

This is the real explaination. One should never assume that comparison are
value-based. Hence that two different arrays are not equal, is working as
designed.

However, on a side-note, when realizing a ValueType class, like a struct in
C#; forgetting to override Equals, GetHashCode and ToString should be a
criminal offence. =)

- Michael S

ps.
Keep up the good work Skeet. I like you posts....
 
However, on a side-note, when realizing a ValueType class, like a struct
in
C#; forgetting to override Equals, GetHashCode and ToString should be a
criminal offence. =)

No. not true at all.

An application even as gracious as the framework cannot uncover every
programmer's intent. In this regard, overriding must be a recommendation
albeit a strong one. But it cannot be manditory as in a criminal offense
which is what you are implying. Throughout the programming life cycle there
may be times when overriding is not needed even for valuetype classes. A
framework needs to be as flexible as possible which is why overriding is
always optional.
 
Back
Top