469,097 Members | 1,304 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,097 developers. It's quick & easy.

Comparing Array Objects

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.
Nov 15 '05 #1
2 1833


Raphael Iloh wrote:
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.


Look at the definition for Object.Equals, it will tell you that for
reference types (which Arrays are) two references are equal if they
refer to the same object. You have two distinct array objects thus the
method yields false.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 15 '05 #2
Martin Honnen <Ma***********@t-online.de> wrote:
Look at the definition for Object.Equals, it will tell you that for
reference types (which Arrays are) two references are equal if they
refer to the same object. You have two distinct array objects thus the
method yields false.


Only if Array doesn't provide an override for the Equals method (as,
for instance, String does). As it happens, it doesn't - but it's not an
unreasonable expectation for it to.

To the OP: you basically have to walk the contents of the arrays to
check whether or not they're equal.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jeff Thies | last post: by
1 post views Thread by Raphael Iloh | last post: by
19 posts views Thread by Dennis | last post: by
5 posts views Thread by ma740988 | last post: by
19 posts views Thread by Ole Nielsby | last post: by
reply views Thread by ccshine via DotNetMonster.com | last post: by
20 posts views Thread by Bill Pursell | last post: by
25 posts views Thread by J Caesar | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by kglaser89 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.