Sign in to follow this  
Daniel Miller

C#: I'm enjoying comparing arrays

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this