1 //SortedList<TKey, TValue>这个链表是一个自动按键值排列的链表 使用foreach遍历时 类型为KeyValuePair<TKey, TValue> 2 //但该链表有一个问题是键值不能重复 这边由于需要键值可以重复 所以必须要定义一个类派生自IComparer<KeyType> 尖括号内为键值的类型 3 4 //IComparer接口中有一个方法Compare 比较两个对象并返回一个值 表示小于 等于或大于另一个对象 5 //在这里我们需要做的就是重写这个方法 使其返回值不等于0 此处用int作为示例 6 public class MySort : IComparer<int> 7 { 8 public int Compare (int x, int y) 9 { 10 int result = x - y; 11 if(result <= 0) 12 { 13 result = -1; 14 } 15 return 1; 16 } 17 } 18 19 void func () 20 { 21 //这样定义的list就是键值可以重复的排序链表 22 SortedList<int, string> list = new SortedList<int, string>(new MySort()); 23 } 24