Home All Groups Group Topic Archive Search About
Author
16 Feb 2005 10:46 PM
Edward Diener
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.
Author
17 Feb 2005 2:45 AM
Jon Skeet [C# MVP]
Edward Diener <diener_no_spam_ok@ORsoftware.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Are all your drivers up to date? click for free checkup

Author
17 Feb 2005 2:40 PM
Edward Diener
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MPG.1c7e159ff852112498bd52@msnews.microsoft.com...
> Edward Diener <diener_no_spam_ok@ORsoftware.com> 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.
Author
17 Feb 2005 5:58 PM
Jon Skeet [C# MVP]
Edward Diener <diener_no_spam_ok@ORsoftware.com> wrote:
Show quoteHide quote
> "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
> news:MPG.1c7e159ff852112498bd52@msnews.microsoft.com...
> > Edward Diener <diener_no_spam_ok@ORsoftware.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Author
17 Feb 2005 6:55 AM
Parahat Melayev
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*/
}


Show quoteHide quote
"Edward Diener" <diener_no_spam_ok@ORsoftware.com> wrote in message
news:OOt1OlHFFHA.3244@TK2MSFTNGP15.phx.gbl...
> 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.
>
>

Bookmark and Share