title: c# inherit dictionary override default property
body: <p>I am trying to make a class that's inherits from Dictionary.</p>
  
  <p>For example:</p>
  
  <pre><code>var retDict = new Dictionary&lt;string,string&gt;();
  </code></pre>
  
  <p>then I can call 
  retDict["test"] to get the item with key "test".</p>
  
  <p>What I want to do is having a method on retDict that I call like this.
  retDict["test","the default return value"];</p>
  
  <p>I want this method to create an item in the base dictionary if the key is not found.</p>
  
  <p>Is this possible? If it is possible, How do I write this method/"peroperty get"?</p>
  
  <p>Best Regards.
  /Dennis</p>
  
  <p>[EDIT]</p>
  
  <p>THis is how I write it VB.NET.</p>
  
  <p><code></p>
  
  <p>Public Class MyDictionary
      Inherits System.Collections.Generic.Dictionary(Of String, String)</p>
  
  <pre><code>Default Public Overloads Property Item(ByVal key As String, Optional DefaultValue As String = "") As String
      Get
          If Not MyBase.ContainsKey(key) Then
              If DefaultValue &lt;&gt; "" Then
                  MyBase.Add(key, DefaultValue)
              Else
                  Return ""
              End If
          End If
          Return MyBase.Item(key)
      End Get
      Set(ByVal value As String)
          MyBase.Item(key) = value
      End Set
  End Property
  </code></pre>
  
  <p>End Class
  </code></p>