HashSet comparison

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
					
public class Program
{
	public static void Main()
	{
		List<float> list = new List<float>() { 10.0f, 15.0f};
		List<float> anotherList = new List<float>() { 10.0f, 15.0f};
		
		Console.WriteLine(list.Equals(anotherList));
		
		HashSet<List<float>> hashSet1 = new HashSet<List<float>>();
		hashSet1.Add(list);
		
		HashSet<List<float>> hashSet2 = new HashSet<List<float>>();
		hashSet2.Add(anotherList);
		
		Console.WriteLine(hashSet1.SetEquals(hashSet2));
		
		
		HashSet<List<float>> anotherHashSet1 = new HashSet<List<float>>(new FloatListComparer());
		anotherHashSet1.Add(list);
		
		HashSet<List<float>> anotherHashSet2 = new HashSet<List<float>>();
		anotherHashSet2.Add(anotherList);
			
		
		Console.WriteLine(anotherHashSet1.SetEquals(anotherHashSet2));
		
		Console.WriteLine(anotherHashSet2.SetEquals(anotherHashSet1));
	}
}

public class FloatListComparer : EqualityComparer<List<float>>
{
    public override bool Equals(List<float> list1, List<float> list2)
    {
        return Enumerable.SequenceEqual(list1.OrderBy(t => t), list2.OrderBy(t => t));
    }


    public override int GetHashCode(List<float> s)
    {
        return base.GetHashCode();
    }
}
;