Jump to content
  • Advertisement
Sign in to follow this  
Daniel Miller

C#: I'm enjoying comparing arrays

This topic is 4851 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!