KeyValuePairList.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. namespace Utilities
  5. {
  6. public partial class KeyValuePairList<TKey, TValue> : List<KeyValuePair<TKey, TValue>>
  7. {
  8. public KeyValuePairList()
  9. {
  10. }
  11. public KeyValuePairList(List<KeyValuePair<TKey, TValue>> collection) : base(collection)
  12. {
  13. }
  14. public bool ContainsKey(TKey key)
  15. {
  16. return (this.IndexOfKey(key) != -1);
  17. }
  18. public int IndexOfKey(TKey key)
  19. {
  20. for (int index = 0; index < this.Count; index++)
  21. {
  22. if (this[index].Key.Equals(key))
  23. {
  24. return index;
  25. }
  26. }
  27. return -1;
  28. }
  29. public TValue ValueOf(TKey key)
  30. {
  31. for (int index = 0; index < this.Count; index++)
  32. {
  33. if (this[index].Key.Equals(key))
  34. {
  35. return this[index].Value;
  36. }
  37. }
  38. return default(TValue);
  39. }
  40. public void Add(TKey key, TValue value)
  41. {
  42. this.Add(new KeyValuePair<TKey, TValue>(key, value));
  43. }
  44. public List<TKey> Keys
  45. {
  46. get
  47. {
  48. List<TKey> result = new List<TKey>();
  49. foreach (KeyValuePair<TKey, TValue> entity in this)
  50. {
  51. result.Add(entity.Key);
  52. }
  53. return result;
  54. }
  55. }
  56. public List<TValue> Values
  57. {
  58. get
  59. {
  60. List<TValue> result = new List<TValue>();
  61. foreach (KeyValuePair<TKey, TValue> entity in this)
  62. {
  63. result.Add(entity.Value);
  64. }
  65. return result;
  66. }
  67. }
  68. new public void Sort()
  69. {
  70. this.Sort(Comparer<TKey>.Default);
  71. }
  72. public void Sort(ListSortDirection sortDirection)
  73. {
  74. Sort(Comparer<TKey>.Default, sortDirection);
  75. }
  76. public void Sort(IComparer<TKey> comparer, ListSortDirection sortDirection)
  77. {
  78. if (sortDirection == ListSortDirection.Ascending)
  79. {
  80. Sort(comparer);
  81. }
  82. else
  83. {
  84. Sort(new ReverseComparer<TKey>(comparer));
  85. }
  86. }
  87. public void Sort(IComparer<TKey> comparer)
  88. {
  89. this.Sort(delegate(KeyValuePair<TKey, TValue> a, KeyValuePair<TKey, TValue> b)
  90. {
  91. return comparer.Compare(a.Key, b.Key);
  92. });
  93. }
  94. public new KeyValuePairList<TKey, TValue> GetRange(int index, int count)
  95. {
  96. return new KeyValuePairList<TKey, TValue>(base.GetRange(index, count));
  97. }
  98. }
  99. }