C# SortedList, getting value by key

Posted by user1004039 on Stack Overflow See other posts from Stack Overflow or by user1004039
Published on 2011-11-19T09:44:05Z Indexed on 2011/11/19 9:51 UTC
Read the original article Hit count: 264

Filed under:
|
|

I have SortedList in descending order.

public class MyComparer : IComparer<int>
    {
        public int Compare(int x, int y)
        {
            if (x.CompareTo(y) > 0)
                return -1;
            return 1;
        }
    }
class Program
{
        static void Main(string[] args)
        {
            SortedList<int, bool> myList = new SortedList<int, bool>(new MyComparer());
            myList.Add(10, true);
            bool check = myList[10];//In this place an exception "Can't find key" occurs
        }
}

When SortedList created without my own IComparer the code works fine and no exception occurs.

© Stack Overflow or respective owner

Related posts about c#

Related posts about sortedlist