title: Store reference to an object in dictionary
body: <p>I have been searching for a way to save the references of variables of various types into a dictionary, together with a corresponding key. Then i would like to modify the instance of the variable by accessing its reference through the dictionary by its key. 
  For storing the references, i tried to use <code>&lt;object&gt;</code>, but without success. Neither Dictionaries nor Lists accept anything like <code>Dictionary&lt;string, ref int&gt;</code>. 
  The following code compiles, but seems to update the variables by value only. Any ideas or workarounds?</p>
  
  <p>Here's the (tested) code:</p>
  
  <pre><code>class Test1
  {
      IDictionary&lt;string, object&gt; MyDict = new Dictionary&lt;string, object&gt;();
  
      public void saveVar(string key, ref int v) //storing the ref to an int
      {
          MyDict.Add(key, v);
      }
      public void saveVar(string key, ref string s) //storing the ref to a string
      {
          MyDict.Add(key, s);
      }
  
      public void changeVar(string key) //changing any of them
      {
          if(MyDict.GetType() == typeof(int))
          {
              MyDict[key] = (int)MyDict[key] * 2;
          }
          if(MyDict.GetType() == typeof(string))
          {
              MyDict[key] = "Hello";
          }
      }
  }
  </code></pre>
  
  <p>And this is how i call the methods of the class</p>
  
  <pre><code>Test1 t1 = new Test1();
  int myInt = 3;
  string myString = "defaultString";
  
  Console.WriteLine(myInt); //returns "3"
  Console.WriteLine(myString); //returns "defaultString"
  
  t1.saveVar("key1", ref myInt);
  t1.saveVar("key2", ref myString);
  
  t1.changeVar("key1");
  t1.changeVar("key2");
  
  Console.WriteLine(myInt); //should return "6"
  Console.WriteLine(myString); //should return "Hello"
  </code></pre>