title: Implementing a Generic method
body: <p>I created an interface  with one method, the ability to copy the contents of one object into another object of the same type (the actual functionality is irrelevant to the question).</p>
  
  <pre><code>public interface IDeepClonable
  {
      void DeepClone&lt;T&gt;(T other);
  }
  </code></pre>
  
  <p>I'm having trouble with a proper implementation.</p>
  
  <p>What I really would like is to implement it like this (where this is inside ClassA, which implements IDeepClonable)</p>
  
  <pre><code>public void DeepClone&lt;ClassA&gt;(ClassA other)
  {
      this.A = other.A;
  }
  </code></pre>
  
  <p>However this does not work, as the 'other' object is not recognized as an instance of ClassA by the compiler (why?)</p>
  
  <p>This does not work either, as it gives a 'the constraints for type parameter T must match the (...) interface method.</p>
  
  <pre><code>public void DeepClone&lt;T&gt;(T other) where T : ClassA
  {
      this.A= other.A;
  }
  </code></pre>
  
  <p>I can solve all problems by changing the interface to take in an object instead of a generic constraint, but I was hoping for a more elegant solution.</p>
  
  <p>I can also solve this by turning the interface into a generic interface, but then then that forces me to cast to that generic interface.</p>