title: C# Generics: Inserting T as the where T : IComparable<T> interface conflict
body: <p>This is a challenge for the C# generics / design patterns masters.</p>
  
  <p>I'm trying to implement a generic heap, and then a priority queue that uses the heap.</p>
  
  <p>My heap's signature is:</p>
  
  <pre><code>class Heap&lt;TKey, TValue&gt; where TKey : IComparable&lt;TKey&gt;
  </code></pre>
  
  <p>My priority queue class is:</p>
  
  <pre><code>public delegate IComparable&lt;T&gt; Evaluator&lt;T&gt;(T item);
  
  class PriorityQueue&lt;T&gt; : IQueue&lt;T&gt;
  {
      Evaluator&lt;T&gt; Evaluate;
      Heap&lt;IComparable&lt;T&gt;, T&gt; m_heap;
  
      public PriorityQueue(Evaluator&lt;T&gt; evaluateFunction)
      {
          Evaluate = evaluateFunction;
          m_heap = new Heap&lt;int, T&gt;(HeapType.MinHeap);
      }
  
      ...
  
      public void Insert(T element)
      {
          m_heap.Insert(Evaluate(element), element);
      }
  
      ...
  </code></pre>
  
  <p>But when doing so, the compiler (justifiably) complains that ICompareble doesn't implement the ICompareble interface, hence</p>
  
  <pre><code>Heap&lt;IComparable&lt;T&gt;, T&gt; m_heap;
  </code></pre>
  
  <p>conflicts with</p>
  
  <pre><code>where TKey : IComparable&lt;TKey&gt;
  </code></pre>
  
  <p><strong>What can you do to solve this?!</strong></p>
  
  <p>full compiler error:</p>
  
  <pre><code>The type 'System.IComparable&lt;T&gt;' cannot be used as type parameter 'TKey' in the generic type or method 'Heap&lt;TKey,TValue&gt;'. There is no implicit reference conversion from 'System.IComparable&lt;T&gt;' to 'System.IComparable&lt;System.IComparable&lt;T&gt;&gt;'.
  </code></pre>