if order doesn't matter or there can be duplicates, then perhaps:
public static class IEnumerableExtensions
{
public static bool HasSameContentsAs<T>(this ICollection<T> source,
ICollection<T> other)
{
if (source.Count != other.Count)
{
return false;
}
var s = source
.GroupBy(x => x)
.ToDictionary(x => x.Key, x => x.Count());
var o = other
.GroupBy(x => x)
.ToDictionary(x => x.Key, x => x.Count());
int count;
return s.Count == o.Count &&
s.All(x => o.TryGetValue(x.Key, out count) &&
count == x.Value);
}
}
usage:
string[] a = { "a", "b", "c" };
string[] b = { "c", "a", "b" };
bool containSame = a.HasSameContentsAs(b);
some use cases:
different lengths (expect false)
string[] a = { "a", "b", "c" };
string[] b = { "b", "c" };
different order (expect true)
string[] a = { "a", "b", "c" };
string[] b = { "b", "c", "a" };
also works if the inputs can contain duplicate items, though it isn't clear from the question whether that characteristic is desired or not, consider:
duplicated items have same count (expect true)
string[] a = { "a", "b", "b", "c" };
string[] b = { "a", "b", "c", "b" };
duplicated items with different counts (expect false)
string[] a = { "a", "b", "b", "b", "c" };
string[] b = { "a", "b", "c", "b", "c" };