title: Get value from Dictionary<string, object> without unboxing?
body: <p>I was wondering if it's possible to run the following code but without the unboxing line:-</p>
  
  <pre><code>t.Value = (T)x;
  </code></pre>
  
  <p>Or maybe if there is another way to do this kind of operation?</p>
  
  <p>Here is the full code:-</p>
  
  <pre><code>public class ValueWrapper&lt;T&gt;
  {
      public T Value { get; set; }
      public bool HasValue { get; set; }
  
      public ValueWrapper()
      {
          HasValue = false;
      }
  }
  
  class Program
  {
      static void Main(string[] args)
      {
          Dictionary&lt;string, object&gt; myDictionary = new Dictionary&lt;string, object&gt;();
  
          myDictionary.Add("key1", 6);
          myDictionary.Add("key2", "a string");
  
          var x2 = GetValue&lt;int&gt;(myDictionary, "key1");
          if (x2.HasValue)
              Console.WriteLine("'{0}' = {1}", "key1", x2.Value);
          else
              Console.WriteLine("No value found");
  
          Console.ReadLine();
      }
  
      static ValueWrapper&lt;T&gt; GetValue&lt;T&gt;(IDictionary&lt;string, object&gt; dictionary, string key)
      {
          ValueWrapper&lt;T&gt; t = new ValueWrapper&lt;T&gt;();
  
          object x = null;
          if (dictionary.TryGetValue(key, out x))
          {
              if (x.GetType() == typeof(T))
              {
                  t.Value = (T)x;
                  t.HasValue = true;
              }
          }
  
          return t;
      }
  }
  </code></pre>
  
  <p>Thanks in advance!!</p>
  
  <p>Richard.</p>