Is this correct and best way to compare large files?
You are not comparing large files, you are just comparing their names. Hashset is perfectly suited to do this operations on sets.
I would not advise using what sbrauen propose
var result = theirHashSet.Where(x => !myHashSet.Contains(x));
because it has to do n operations on m entries, n and m being number of entries in theirHashSet
and myHashSet
respectively. Hashset should be more performing with these actions. And what is actually better then Except
is ExceptWith
because Except
is an extension method of IEnumerable
whereas ExceptWith
is a method in HashSet<>
.
EDIT:
The difference is that Except
returns a new IEnumerable
collection whereas ExceptWith
will remove equal entries from theirHashSet
. Also ExceptWith
is faster because it knows internals of HashTable
, Except
is just an extension method.
Here is what it looks like under the hood
Except
Set<TSource> set = new Set<TSource>(comparer);
foreach (TSource tSource in second)
{
set.Add(tSource);
}
foreach (TSource tSource1 in first)
{
if (!set.Add(tSource1))
{
continue;
}
yield return tSource1;
}
ExceptWith
foreach (T t in other)
{
this.Remove(t);
}
You can see a difference immediately.