If you could actually keep one of them as an ISet<T>
, you can just call SetEquals
. You could do this optionally, of course:
ISet<Foo> setFoo = theirs as ISet<Foo>;
if (setFoo != null && theirs.SetEquals(mine))
{
...
}
// Not a set? Use the slower approach
You don't have to sort the set though - you could do:
if (new HashSet<Foo>(theirs).SetEquals(mine))
for example. As noted in comments, this will treat { 1, 1, 2 } as equal to { 1, 2, 2 } though. Only use set operations if you really have a logical set.