title: Casting return value to a generic type
body: <p>Suppose we have an interface with a single generic method:</p>
  
  <pre><code>public interface IExtender
  {
      T GetValue&lt;T&gt;(string tag);
  }
  </code></pre>
  
  <p>and a simple implementation A of it that returns instances of two different types (B and C) depending on the "tag" parameter:</p>
  
  <pre><code>public class A : IExtender
  {
      public T GetValue&lt;T&gt;(string tag)
      {
          if (typeof(T) == typeof(B) &amp;&amp; tag == null)
              return (T)(object) new B();
          if (typeof(T) == typeof(C) &amp;&amp; tag == "foo")
              return (T)(object) new C();
          return default(T);
      }
  }
  </code></pre>
  
  <p>is it possible to avoid the double cast <code>(T)(object)</code>? Or, is there a way to tell the compiler "hey, I am sure that this cast won't fail at runtime, just let me do it without first casting to object!"</p>