Short answer, it depends upon your object's Equals method.
Longer answer:
HashSet will use an IEqualityComparer to determine if two objects are equal. If you don't provide one it will use EqualityComparer.Default
; which effectively just uses object.Equals(a, b) plus some stuff to avoid boxing value types.
Checking the docs for object.Equals(a, b)
: It is effectively just performing a.Equals(b)
after checking for nulls.
The default implementation of object.Equals(other)
is to check for reference equality only (i.e., they are the exact same instance of an object) but you can override this to perform any check you like, such as checking if an ID field is identical. Note, when overriding Equals you also have to override GetHashCode.
If you want to change how HashSet determines equality without altering the object's definition you can provide it a custom IEqualityComparer instead.