title: C# way to mimic Python Dictionary Syntax
body: <p>Is there a good way in C# to mimic the following python syntax:</p>
  
  <pre><code>mydict = {}
  mydict["bc"] = {}
  mydict["bc"]["de"] = "123";  # &lt;-- This line
  mydict["te"] = "5";          # &lt;-- While also allowing this line
  </code></pre>
  
  <p>In other words, I'd like something with [] style access that can return either another dictionary or a string type, depending on how it has been set.</p>
  
  <p>I've been trying to work this out with a custom class but can't seem to succeed.  Any ideas?</p>
  
  <p>Thanks!</p>
  
  <p>Edit:  I'm being evil, I know.  Jared Par's solution is great . . . for a 2-level dictionary of this form.  However, I am also curious about further levels . . . for instance,</p>
  
  <pre><code>mydict["bc"]["df"]["ic"] = "32";
  </code></pre>
  
  <p>And so on.  Any ideas about that?</p>
  
  <p>Edit 3:</p>
  
  <p>Here is the final class I ended up using:</p>
  
  <pre><code>class PythonDict {
      /* Public properties and conversions */
      public PythonDict this[String index] {
          get {
              return this.dict_[index];
          }
          set {
              this.dict_[index] = value;
          }
      }
  
      public static implicit operator PythonDict(String value) {
          return new PythonDict(value);
      }
  
      public static implicit operator String(PythonDict value) {
          return value.str_;
      }
  
      /* Public methods */
      public PythonDict() {
          this.dict_ = new Dictionary&lt;String, PythonDict&gt;();
      }
  
      public PythonDict(String value) {
          this.str_ = value;
      }
  
      public bool isString() {
          return (this.str_ != null);
      }
  
      /* Private fields */
      Dictionary&lt;String, PythonDict&gt; dict_ = null;
      String str_ = null;
  }
  </code></pre>
  
  <p>This class works for infinite levels, and can be read from without explicit conversion (dangerous, maybe, but hey).</p>
  
  <p>Usage like so:</p>
  
  <pre><code>        PythonDict s = new PythonDict();
          s["Hello"] = new PythonDict();
          s["Hello"]["32"] = "hey there";
          s["Hello"]["34"] = new PythonDict();
          s["Hello"]["34"]["Section"] = "Your face";
          String result = s["Hello"]["34"]["Section"];
          s["Hi there"] = "hey";
  </code></pre>
  
  <p>Thank you very much Jared Par!</p>