title: Why can't I cast one instantiation of a generic type to another?
body: <p>How can I implement a struct so that the following cast can be performed?</p>
  
  <pre><code>var a = new StatusedValue&lt;double&gt;(1, false);
  var b = (StatusedValue&lt;int&gt;)a;
  </code></pre>
  
  <p>My implementation should behave similarly to <code>Nullable&lt;T&gt;</code>, which works fine. However, this code fails with <code>System.InvalidCastException</code>:</p>
  
  <pre><code>public struct StatusedValue&lt;T&gt;  where T : struct
  {
      public StatusedValue(T value) : this(value, true)
      {
  
      }
  
      public StatusedValue(T value, bool isValid)
      {
          this.value = value;
          this.isValid = isValid;
      }
  
      private T value;
      private bool isValid;
  
      public static implicit operator StatusedValue&lt;T&gt;(T value)
      {
          return new StatusedValue&lt;T&gt;(value);
      }
  
      public static explicit operator T(StatusedValue&lt;T&gt; value)
      {
          return value.value;
      }
  }
  </code></pre>
  
  <p>Result:</p>
  
  <blockquote>
    <p>Unable to cast object of type 'StatusedValue`1[System.Double]' to type
    'StatusedValue`1[System.Int32]'.</p>
  </blockquote>