PC Review


Reply
Thread Tools Rate Thread

Array equality

 
 
Edward Diener
Guest
Posts: n/a
 
      16th Feb 2005
Is there any System.Array function to test to see if two arrays are equal,
meaning the arrays are the same length and contain elements which are equal
to each other ? The System.Array.Equals method appears to test only for
instance equality, just like object.Equals.


 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      17th Feb 2005
Edward Diener <(E-Mail Removed)> wrote:
> Is there any System.Array function to test to see if two arrays are
> equal, meaning the arrays are the same length and contain elements
> which are equal to each other ? The System.Array.Equals method
> appears to test only for instance equality, just like object.Equals.


No, there isn't - but the good news is that it's pretty trivial to
write your own utility method to do it.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Parahat Melayev
Guest
Posts: n/a
 
      17th Feb 2005
it is easy write write your own

if(arr1.Length == arr2.Length)
{
for(int i=0; i<arr1.Length; i++)
if(arr1[i] != arr2[i])
/* not same*/
}


"Edward Diener" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Is there any System.Array function to test to see if two arrays are equal,
> meaning the arrays are the same length and contain elements which are

equal
> to each other ? The System.Array.Equals method appears to test only for
> instance equality, just like object.Equals.
>
>



 
Reply With Quote
 
Edward Diener
Guest
Posts: n/a
 
      17th Feb 2005
"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Edward Diener <(E-Mail Removed)> wrote:
>> Is there any System.Array function to test to see if two arrays are
>> equal, meaning the arrays are the same length and contain elements
>> which are equal to each other ? The System.Array.Equals method
>> appears to test only for instance equality, just like object.Equals.

>
> No, there isn't - but the good news is that it's pretty trivial to
> write your own utility method to do it.


Yes, I realize it is pretty trivial to do it. Perhaps I do not understand
the purpose of the overridden System.Object.Equals method but I would have
thought that this should test for object equality rather than instance
equality for derived classes, since one could always test for instance
equality by casting both references to a System.Object.


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      17th Feb 2005
Edward Diener <(E-Mail Removed)> wrote:
> "Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Edward Diener <(E-Mail Removed)> wrote:
> >> Is there any System.Array function to test to see if two arrays are
> >> equal, meaning the arrays are the same length and contain elements
> >> which are equal to each other ? The System.Array.Equals method
> >> appears to test only for instance equality, just like object.Equals.

> >
> > No, there isn't - but the good news is that it's pretty trivial to
> > write your own utility method to do it.

>
> Yes, I realize it is pretty trivial to do it. Perhaps I do not understand
> the purpose of the overridden System.Object.Equals method but I would have
> thought that this should test for object equality rather than instance
> equality for derived classes, since one could always test for instance
> equality by casting both references to a System.Object.


Yes, it would make sense for them to have overridden Equals in the
obvious way - I'm not sure why they didn't.

There's nothing to say that classes *have* to override Equals - and
indeed often it's a bad idea to override it for mutable types like
Array. (If you look at GetHashcode, that suggests that the hashcode
shouldn't change which means it can't use any mutable properties of the
object - but two objects which are equal should have the same hashcode.
It's all a bit difficult, partly because I believe the GetHashcode
contract has been badly written.)

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Value equality tshad Microsoft C# .NET 4 14th Dec 2008 10:43 AM
value equality Marcel Sottnik Microsoft C# .NET 2 29th Sep 2004 03:47 PM
equality benben Microsoft C# .NET 6 16th May 2004 03:17 PM
Array Equality Tony Nassar Microsoft C# .NET 3 26th Nov 2003 02:15 PM
Question about equality Matt Burland Microsoft C# .NET 4 12th Nov 2003 10:36 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:33 PM.