title: IComparer<> and class inheritance in C#
body: <p>Is there any way to implement specialized IComparer for the base class type so a child class could still use it for sorting in speciliazed collections?</p>
  
  <p>Example</p>
  
  <pre><code>public class A
  {
     public int X;
  }
  
  public class B:A
  {
     public int Y;
  }
  
  public AComparer:IComparer&lt;A&gt;
  {
      int Compare(A p1, A p2)
      {
          //...
      }
  
  }
  </code></pre>
  
  <p>so following code will work:</p>
  
  <pre><code>List&lt;A&gt; aList = new List&lt;A&gt;();
  aList.Sort(new AComparer());
  
  List&lt;B&gt; bList = new List&lt;B&gt;();
  bList.Sort(new AComparer()); // &lt;- this line fails due to type cast issues 
  </code></pre>
  
  <p>How to approach this issue to have both - inheritance of sorting and  specialized collections (and do not copy IComparer classes for each of children classes?</p>
  
  <p>Thanks in advance!</p>