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 8359369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:07:27+00:00 2026-06-09T11:07:27+00:00

Okay, I’ve been instructed that due to contravariance the compiler should be able to

  • 0

Okay, I’ve been instructed that due to contravariance the compiler should be able to infer that T=Circle and therefore allow compilation. However, with compiler version 4.0.30319.1 I get the following error:

error CS0311: The type ‘Testable.Shape’ cannot be used as type parameter ‘T’ in the generic type or method ‘Testable.Test.Foo(System.IComparable, T)’. There is no implicit reference conversion from ‘Testable.Shape’ to ‘Testable.ICircle’.

The code, however, compiles normally if I implement IComparable on the Circle class. What might be the problem here?

using System; 

namespace Testable 
{ 
    public class Test 
    { 
        public static void Main() 
        { 
            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 Shape : IComparable<Shape>
    { 
        public Int32 CompareTo(Shape other) 
        { 
            Console.WriteLine("Called CompareTo(Shape)"); 
            return 0; 
        } 
    } 

    public class Circle : Shape, ICircle 
    { 
    } 
}
  • 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-09T11:07:29+00:00Added an answer on June 9, 2026 at 11:07 am

    The problem is that Circle implements IComparable<Shape>, and so the compiler chooses Shape for T. However, Shape does not implement ICircle, and so you get the error you see.

    I suppose that begs the question, though. Why doesn’t the compiler revise its guess to Circle once it sees the ICircle constraint. The answer is that constraints are not used to aid in type inference. They can only invalidate a guess after it has been made.

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

Sidebar

Related Questions

Okay, so I know that using eval() isn't great, but I haven't been able
Okay, next PHPExcel question. I have an HTML form that users fill out and
okay, i'm setting up a multi-user chat system. i have a messages table, that
okay so I have two arrays $array_one([a]=>2,[b]=>1,[c]=>1); $array_two([a]=>1,[b]=>2,[c]=>1); I want to be able to
Okay,I've been following this tutorial http://coenraets.org/blog/android-samples/androidtutorial/ Basically it gives me exactly what i need
Okay I am having some problems with being able to change bitmaps when a
Okay so I currently have: /(#([\]))/g; I want to be able to check for
Okay, this should be really simple, but I have searched all over for the
Okay I do apologise that I'm out of my depth here. I'm not the
Okay, so this is the line that's returning null. What am I doing wrong

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.