I am trying to play around with sorted sets in c# for a custom objects and for some reason, it seems like the sorted sets might not be using the references of the objects to store the data..
In the following code snippet, I use a custom IComparer to rely on the Counts property of the custom class. But for some reason, this seems to affect the add functionality. and the counter.Add(two) line does not make any addition to the set even though it is a different reference and has a different value for two properties.
Am I missing something? Have I got something wrong about how SortedSets are supposed to work in C#?
Code Snippet
public class SortedStructureTesting
{
public void TestingSortedSets()
{
SortedSet<CounterSetup> counter = new SortedSet<CounterSetup>(new CompareCounts());
CounterSetup one = new CounterSetup(1);
CounterSetup two = new CounterSetup(2);
CounterSetup three = new CounterSetup(3, 2);
counter.Add(one);
counter.Add(two); // Does not work. This value does not get added to the set.
counter.Add(three);
var max = counter.Max;
counter.Remove(max);
var sec = counter.Max;
counter.Remove(sec);
}
public class CounterSetup
{
public static Random random = new Random();
public CounterSetup(int no, int cnt = 1)
{
Number = no;
Count = cnt;
Blah = new string(Guid.NewGuid().ToString());
}
public int Number { get; private set; }
public int Count { get; set; }
public string Blah { get; private set; }
}
public class CompareCounts : IComparer<CounterSetup>
{
public int Compare(CounterSetup one, CounterSetup two)
{
return one.Count.CompareTo(two.Count);
}
}
}
Thanks for taking a look and helping!