KeyValuePairList.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 bool ContainsKey(TKey key)
  9. {
  10. return (this.IndexOfKey(key) != -1);
  11. }
  12. public int IndexOfKey(TKey key)
  13. {
  14. for (int index = 0; index < this.Count; index++)
  15. {
  16. if (this[index].Key.Equals(key))
  17. {
  18. return index;
  19. }
  20. }
  21. return -1;
  22. }
  23. public TValue ValueOf(TKey key)
  24. {
  25. for (int index = 0; index < this.Count; index++)
  26. {
  27. if (this[index].Key.Equals(key))
  28. {
  29. return this[index].Value;
  30. }
  31. }
  32. return default(TValue);
  33. }
  34. public void Add(TKey key, TValue value)
  35. {
  36. this.Add(new KeyValuePair<TKey, TValue>(key, value));
  37. }
  38. public List<TKey> Keys
  39. {
  40. get
  41. {
  42. List<TKey> result = new List<TKey>();
  43. foreach (KeyValuePair<TKey, TValue> entity in this)
  44. {
  45. result.Add(entity.Key);
  46. }
  47. return result;
  48. }
  49. }
  50. public List<TValue> Values
  51. {
  52. get
  53. {
  54. List<TValue> result = new List<TValue>();
  55. foreach (KeyValuePair<TKey, TValue> entity in this)
  56. {
  57. result.Add(entity.Value);
  58. }
  59. return result;
  60. }
  61. }
  62. new public void Sort()
  63. {
  64. this.Sort(Comparer<TKey>.Default);
  65. }
  66. public void Sort(ListSortDirection sortDirection)
  67. {
  68. Sort(Comparer<TKey>.Default, sortDirection);
  69. }
  70. public void Sort(IComparer<TKey> comparer, ListSortDirection sortDirection)
  71. {
  72. if (sortDirection == ListSortDirection.Ascending)
  73. {
  74. Sort(comparer);
  75. }
  76. else
  77. {
  78. Sort(new ReverseComparer<TKey>(comparer));
  79. }
  80. }
  81. public void Sort(IComparer<TKey> comparer)
  82. {
  83. this.Sort(delegate(KeyValuePair<TKey, TValue> a, KeyValuePair<TKey, TValue> b)
  84. {
  85. return comparer.Compare(a.Key, b.Key);
  86. });
  87. }
  88. }
  89. }