Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>While the answer of Kev is perfectly correct and given on a professional level it still gave me a hard time (and initiated a lot of fruitful learning - thanks Kev!). As you can tell I am a learner of C# and there are many concepts that I still have to assimilate. I want to add an answer to my own question here in case anybody else has the same problem and is on a similar level of understanding as me. Maybe this will save some livetime.</p> <p>Kev used Generics in his answer - a great concept introduced with C#2. To simplify the answer I want to show it without Generics and with a lot of comments added that give hints to all concepts that I had to look up (and that were partially not easy to find):</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DictionaryElementsGetter_Test { // class inherits from Dictionary&lt;int, string&gt; public class MyDictionary : Dictionary&lt;int, string&gt; { // new: hide element of base class to redefine it in derived class. // see http://msdn.microsoft.com/en-us/library/435f1dw2.aspx // string this[int key]: create an indexer // (actually: replace the indexer of base class, because of "new") // see http://msdn.microsoft.com/en-us/library/2549tw02.aspx new public string this[int key] { get { string value; // out: pass argument by reference instead of by value // This is the standard definition of TryGetValue. // Beside the bool result that indicates the existence of the key-value-pair, // TryGetValue also returns the value itself into this reference parameter (if key is found). // see http://msdn.microsoft.com/en-us/library/ee332485.aspx if( !TryGetValue( key, out value ) ) { value = "abc" + key + "def"; Add( key, value ); // just to see when the getter really did an Add(): Console.Write( "ADDED!... " ); } return value; } // base: access element of the base class Dictionary&lt;int, string&gt; // see http://msdn.microsoft.com/en-us/library/hfw7t1ce(v=vs.100).aspx set { base[key] = value; } } } class Program { static void Main( string[] args ) { var dict = new MyDictionary(); dict[1] = "EXTERNAL VALUE"; for( int i = 0; i &lt; 3; i++ ) { Console.WriteLine( i + ": " + dict[i] ); } /* Output: ADDED!... 0: abc0def 1: EXTERNAL VALUE ADDED!... 2: abc2def */ for( int i = 0; i &lt; 3; i++ ) { Console.WriteLine( i + ": " + dict[i] ); } /* Output: 0: abc0def 1: EXTERNAL VALUE 2: abc2def */ Console.ReadKey(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload