title: C# Casting generics (covariance and contravariance?)
body: <p>I need some advice/help on this, I can't see the wood from the trees any more.</p>
  
  <p>It's a straight forward series of classes implementing some interfaces using generics.</p>
  
  <p>Then I'm trying to cast the concrete types for example:</p>
  
  <pre><code>MyGenericObject&lt;SomeObject&gt; _obj;
  
  IMyGenericObject&lt;ISomeObject&gt; _genObj = (IMyGenericObject&lt;ISomeObject&gt;)_obj;
  </code></pre>
  
  <p>// Invalid cast</p>
  
  <p>I've read some articles about covariance and contravariance but not too clear why this wouldn't be possible, or how to get round it?</p>
  
  <hr>
  
  <p>So, in this example:</p>
  
  <pre><code>public interface IMyObject&lt;in T&gt; where T : IBaseObject
  {
      T Activity { get; set; }
  }
  </code></pre>
  
  <p>wouldn't work... </p>
  
  <hr>
  
  <p>....because, you can't get and set the Activity property.</p>
  
  <p>In this example, I needed to do:</p>
  
  <pre><code>public interface IMyObject&lt;out T&gt; where T : IBaseObject
      {
          T Activity { get; }
      }
  </code></pre>
  
  <p>hope that helps someone, and thanks to all for help!</p>