赞
踩
- public class IComparer
- {
- /// <summary>
- /// Comparer for comparing two keys, handling equality as beeing greater
- /// Use this Comparer e.g. with SortedLists or SortedDictionaries, that don't allow duplicate keys
- /// </summary>
- /// <typeparam name="TKey"></typeparam>
- public class DuplicateKeyComparer<TKey>
- :
- IComparer<TKey> where TKey : IComparable
- {
- #region IComparer<TKey> Members
-
- public int Compare(TKey x, TKey y)
- {
- int result = x.CompareTo(y);
-
- if (result == 0)
- return 1; // Handle equality as being greater. Note: this will break Remove(key) or
- else // IndexOfKey(key) since the comparer never returns 0 to signal key equality
- return result;
- }
-
- #endregion
- }
- }

- SortedList<int, string> list = new SortedList<int, string>(new DuplicateKeyComparer<int>());
-
- list.Add(1,"123");
- list.Add(1,"456");
- list.Add(1,"789");
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。