Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8350615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:22:14+00:00 2026-06-09T08:22:14+00:00

Earlier, I was provided a concrete example of contravariance in the generic IComparable<T> interface

  • 0

Earlier, I was provided a concrete example of contravariance in the generic IComparable<T> interface by Jon Skeet. This has, however, spawned yet another question. Why isn’t the generic List<T>.Sort() method able to infer the same information?

I’ve provided the referenced example here as the static Foo<T>() method. You’ll observe that this method is able to infer the type of T and call CompareTo(Circle). List<ICircle>.Sort() on the other hand is unable to infer the type of T and so calls CompareTo(Object).

using System;
using System.Collections.Generic;

namespace Testable
{
    public class Test
    {
        public static void Main()
        {
            List<ICircle> circles = new List<ICircle>();

            circles.Add(new Circle());
            circles.Add(new Circle());

            circles.Sort();

            Foo(new Circle(), new Circle());
        }

        public static void Foo<T>(IComparable<T> a, T b) where T : ICircle
        {
            a.CompareTo(b);
        }
    }

    public interface ICircle
    {
    }

    public class Circle :
    IComparable, IComparable<Circle>, ICircle
    {
        public Int32 CompareTo(Object other)
        {
            Console.WriteLine("Called CompareTo(Object)");
            return 0;
        }

        public Int32 CompareTo(Circle other)
        {
            Console.WriteLine("Called CompareTo(Circle)");
            return 0;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T08:22:15+00:00Added an answer on June 9, 2026 at 8:22 am

    The list is a list of ICircle, not a list of Circle. ICircle doesn’t implement IComparable<Circle> or IComparable<ICircle> (or any other IComparable interface. All of the items that you put into the list do happen to implement IComparer<Circle> and be Circles, but you could just as easily put an item in that implemented ICircle and implemented no IComparable interfaces at all.

    Foo on the other hand takes an IComparable<T> as a parameter, so you know that it implements IComparable<T>.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I asked this question earlier & was provided with some code, however it refuses
I know this question has been asked before for earlier versions of Visual Studio
Earlier this week, I had to do something which feels like a semantics violation.
This is a follow up to a question I asked earlier seen here: Confused
I'm new to this HL7 standard, but earlier I worked on NCPDP standard. Now
I have asked this question earlier today but I didn't provide enough information and
This is a more focused question triggered by an earlier posting here . I
This question is related to my earlier question, asked here: How do I get
I asked this question earlier How to change value of com.arjuna.ats.jbossatx.jta.TransactionManagerService TransactionTimeout at the
As title states, my piece of code which was from an example provided by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.