当前位置:   article > 正文

允许重复键的 C# 可排序集合_c#中可以存储重复key的

c#中可以存储重复key的
  1. public class IComparer
  2. {
  3. /// <summary>
  4. /// Comparer for comparing two keys, handling equality as beeing greater
  5. /// Use this Comparer e.g. with SortedLists or SortedDictionaries, that don't allow duplicate keys
  6. /// </summary>
  7. /// <typeparam name="TKey"></typeparam>
  8. public class DuplicateKeyComparer<TKey>
  9. :
  10. IComparer<TKey> where TKey : IComparable
  11. {
  12. #region IComparer<TKey> Members
  13. public int Compare(TKey x, TKey y)
  14. {
  15. int result = x.CompareTo(y);
  16. if (result == 0)
  17. return 1; // Handle equality as being greater. Note: this will break Remove(key) or
  18. else // IndexOfKey(key) since the comparer never returns 0 to signal key equality
  19. return result;
  20. }
  21. #endregion
  22. }
  23. }
  1. SortedList<int, string> list = new SortedList<int, string>(new DuplicateKeyComparer<int>());
  2. list.Add(1,"123");
  3. list.Add(1,"456");
  4. list.Add(1,"789");

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/977641
推荐阅读
相关标签
  

闽ICP备14008679号