For keeping "unique" lists you should define what "equal" lists are. The typical way is to define IEqualityComparer<T>
, e.g.
public class MyComparer : IEqualityComparer<List<int>> {
public bool Equals(List<int> A, List<int> B) {
if (ReferenceEquals(A, B))
return true;
if (A is null || B is null)
return false;
return A.SequenceEqual(B);
}
//TODO: easiest, but not the best hash function
public int GetHashCode(List<int> list) => list is null
? -1
: list.Count;
}
Then you should mention this comparer when creating HashSet
:
var hs = new HashSet<List<int>>(new MyComparer());
List<int> list = new List<int>() { 1, 2, 3 };
hs.Add(new List<int>(){1, 2, 3});
hs.Add(list);
Console.WriteLine(hs.Count);
Output:
1
Please, fiddle yourself.