Question

I'm trying to use SortedList with case-insensitive string comparison. The following is working:

SortedList mySL = new SortedList(new CaseInsensitiveComparer());
mySL.Add("key_1", "val_1");
mySL.Add("key_2", "val_2");
mySL.Add("key_3", "val_3");
if (mySL.ContainsKey("KEY_1"))
    MessageBox.Show("is there"); // message appears
else
    MessageBox.Show("not found");

But this is not:

public class MySL : SortedList
{
    // The only constructor
    public MySL(IComparer comparer) {}

    ...
}

MySL sl = new MySL(new CaseInsensitiveComparer());

sl.Add("key_1", "val_1");
sl.Add("key_2", "val_2");
sl.Add("key_3", "val_3");
if (sl.ContainsKey("KEY_1"))
    MessageBox.Show("is there"); 
else
    MessageBox.Show("not found"); // message appears

Can anybody see what's wrong?

Was it helpful?

Solution

You need to pass the comparer to the base class constructor:

public MySL(IComparer comparer)
    : base(comparer) { }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top