Guest User

Untitled

a guest
May 5th, 2012
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. A getter for dictionary elements in C# 4.0
  2. public class MyDictionary<TKey, TValue> : Dictionary<TKey, TValue>
  3. {
  4. public new TValue this[TKey key]
  5. {
  6. get
  7. {
  8. TValue value;
  9. if (!TryGetValue(key, out value))
  10. {
  11. value = Activator.CreateInstance<TValue>();
  12. Add(key, value);
  13. }
  14. return value;
  15. }
  16. set { base[key] = value; }
  17. }
  18. }
  19.  
  20. public class MyDictionary<TKey, TValue> : Dictionary<TKey, TValue>
  21. {
  22. readonly Func<TKey, TValue> _activator;
  23.  
  24. public MyDictionary(Func<TKey, TValue> activator)
  25. {
  26. _activator = activator;
  27. }
  28.  
  29. public new TValue this[TKey key]
  30. {
  31. get
  32. {
  33. TValue value;
  34. if (!TryGetValue(key, out value))
  35. {
  36. value = _activator(key);
  37. Add(key, value);
  38. }
  39. return value;
  40. }
  41. set { base[key] = value; }
  42. }
  43. }
  44.  
  45. static void Main(string[] args)
  46. {
  47. var dict = new MyDictionary<int, string>(x => string.Format("Automatically created Value for key {0}", x));
  48. dict[1] = "Value for key 1";
  49. for (int i = 0; i < 3; i++)
  50. {
  51. Console.WriteLine(dict[i]);
  52. }
  53. Console.Read();
  54. }
  55.  
  56. using System;
  57. using System.Collections.Generic;
  58. using System.Linq;
  59. using System.Text;
  60.  
  61. namespace DictionaryElementsGetter_Test {
  62.  
  63. // class inherits from Dictionary<int, string>
  64. public class MyDictionary : Dictionary<int, string> {
  65. // new: hide element of base class to redefine it in derived class.
  66. // see http://msdn.microsoft.com/en-us/library/435f1dw2.aspx
  67. // string this[int key]: create an indexer
  68. // (actually: replace the indexer of base class, because of "new")
  69. // see http://msdn.microsoft.com/en-us/library/2549tw02.aspx
  70. new public string this[int key] {
  71. get {
  72. string value;
  73. // out: pass argument by reference instead of by value
  74. // This is the standard definition of TryGetValue.
  75. // Beside the bool result that indicates the existence of the key-value-pair,
  76. // TryGetValue also returns the value itself into this reference parameter (if key is found).
  77. // see http://msdn.microsoft.com/en-us/library/ee332485.aspx
  78. if( !TryGetValue( key, out value ) ) {
  79. value = "abc" + key + "def";
  80. Add( key, value );
  81. // just to see when the getter really did an Add():
  82. Console.Write( "ADDED!... " );
  83. }
  84. return value;
  85. }
  86. // base: access element of the base class Dictionary<int, string>
  87. // see http://msdn.microsoft.com/en-us/library/hfw7t1ce(v=vs.100).aspx
  88. set { base[key] = value; }
  89. }
  90. }
  91.  
  92.  
  93. class Program {
  94. static void Main( string[] args ) {
  95.  
  96. var dict = new MyDictionary();
  97. dict[1] = "EXTERNAL VALUE";
  98.  
  99. for( int i = 0; i < 3; i++ ) {
  100. Console.WriteLine( i + ": " + dict[i] );
  101. }
  102. /*
  103. Output:
  104. ADDED!... 0: abc0def
  105. 1: EXTERNAL VALUE
  106. ADDED!... 2: abc2def
  107. */
  108. for( int i = 0; i < 3; i++ ) {
  109. Console.WriteLine( i + ": " + dict[i] );
  110. }
  111. /*
  112. Output:
  113. 0: abc0def
  114. 1: EXTERNAL VALUE
  115. 2: abc2def
  116. */
  117.  
  118. Console.ReadKey();
  119.  
  120. }
  121. }
  122. }
  123.  
  124. Dictionary<int, string> _myDictionary = new Dictionary<int, string>();
  125.  
  126. _myDictionary[1] = "Hello";
  127. _myDictionary[2] = "World!";
Advertisement
Add Comment
Please, Sign In to add comment