tags:

views:

43

answers:

2

I want to check if an object is in a Queue before I enqueue it. If don't explicitly define an EqualityComparer, what does the Contains() function compare?

If it compares property values, that's perfect. If it compares to see if a reference to that object exists in the Queue then that defeats the purpose.

+7  A: 

For classes, the default equality operation is by reference - it assumes that object identity and equality are the same, basically.

You can overcome this by overriding Equals and GetHashCode. I'd also suggest implementing IEquatable<T> to make this clear. Your hash code implementation should generate the hash code from the same values as the equality operation.

Jon Skeet
A: 

The default for reference types is to compare the reference.

However, if the type implements IEquatable<> it can be doing a different comparison. If you need to have a specific equality comparison in place, you need to create one yourself.

Lucero