KeyValuePairList.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Utilities
  4. {
  5. public partial class KeyValuePairList<TKey, TValue> : List<KeyValuePair<TKey, TValue>>
  6. {
  7. public bool ContainsKey(TKey key)
  8. {
  9. return (this.IndexOfKey(key) != -1);
  10. }
  11. public int IndexOfKey(TKey key)
  12. {
  13. for (int index = 0; index < this.Count; index++)
  14. {
  15. if (this[index].Key.Equals(key))
  16. {
  17. return index;
  18. }
  19. }
  20. return -1;
  21. }
  22. public TValue ValueOf(TKey key)
  23. {
  24. for (int index = 0; index < this.Count; index++)
  25. {
  26. if (this[index].Key.Equals(key))
  27. {
  28. return this[index].Value;
  29. }
  30. }
  31. return default(TValue);
  32. }
  33. public void Add(TKey key, TValue value)
  34. {
  35. this.Add(new KeyValuePair<TKey, TValue>(key, value));
  36. }
  37. public List<TKey> Keys
  38. {
  39. get
  40. {
  41. List<TKey> result = new List<TKey>();
  42. foreach (KeyValuePair<TKey, TValue> entity in this)
  43. {
  44. result.Add(entity.Key);
  45. }
  46. return result;
  47. }
  48. }
  49. public List<TValue> Values
  50. {
  51. get
  52. {
  53. List<TValue> result = new List<TValue>();
  54. foreach (KeyValuePair<TKey, TValue> entity in this)
  55. {
  56. result.Add(entity.Value);
  57. }
  58. return result;
  59. }
  60. }
  61. }
  62. }