C#: I'm enjoying comparing arrays

Started by
5 comments, last by Daniel Miller 18 years, 8 months ago
OK, is there a better way to compare if an array's elements are all equal to another array's elements than manually iterating through both of them and comparing each element?
Advertisement
Not that I know of. You can check the Length property as a quick initial check. I'd just do the brute force technique for now. Of course, if you're bottlenecked there, you can just construct a hashtable out of one array's elements and use it to check the other's.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Ah, thanks.

I'm in the middle of rewqriting a slow tool I had made, and this operation is in the middle of everything. Since I am using arrays, I won't need to do any casting (I'm limited to .NET 1.1).

I'll see if it's still slow as hell once I'm done.
1. Compare the hash values for the tables. If they are the same, check using brute-force.
2. When doing brute-force checking, compare the hash values of the objects before checking real equality.
If arr and arr2 are of type Array then:

if (arr.Equals(arr2))// they are equalelse// they are not equal
Wouldn't that only be shallow equality? I.E. it would return true in this case:
Object[] objects = new Object[10];Object[] objects2 = objects;return objects.Equals(objects2) // TRUE;

Wouldn't this be false though?
Object objects = new Object[]{a, b, c};Object objects2 = new Object[]{a, b, c};return objects.Equals(objects2) // FALSE


Correct me if I'm wrong.
Quote:Original post by Optus
Wouldn't that only be shallow equality? I.E. it would return true in this case:
Object[] objects = new Object[10];Object[] objects2 = objects;return objects.Equals(objects2) // TRUE;

Wouldn't this be false though?
Object objects = new Object[]{a, b, c};Object objects2 = new Object[]{a, b, c};return objects.Equals(objects2) // FALSE


Correct me if I'm wrong.


Yep, because arrays are reference types, and I don't think they changed the semantics of Equals.

This topic is closed to new replies.

Advertisement