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

  • Home
  • SEARCH
  • 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 3853334
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:27:12+00:00 2026-05-19T17:27:12+00:00

I have the following classes and I am trying to call Compare method from

  • 0

I have the following classes and I am trying to call Compare method from ExportFileBaseBL class but I get the error

Cannot implicitly convert type ‘Class1’ to ‘T’. An explicit conversion exists (are you missing a cast?)

public abstract class Class1<T> where T: Class2
{
    public abstract Class1<T> Compare(Class1<T> otherObj);
}

public abstract class Class3<T, U> where T: Class1<U>
                         where U: Class2
{
    public T Compare(T obj1, T obj2)
    {
        if (obj1.Prop1 > obj2.Prop1)
        {
            return obj1.Compare(obj2); // Compiler Error here
        }
        else
        {
            return obj2.Compare(obj1);  // Compiler Error here
        }
    }

}

Shouldn’t the type conversion be implicit? Am I missing something?

  • 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-05-19T17:27:13+00:00Added an answer on May 19, 2026 at 5:27 pm

    The problem is that your abstract Compare method is defined to accept a parameter of type Class1<T> and return an instance of Class1<T>, not a more specific type than Class1<T>. But this is what your Class3.Compare method is attempting to do: call T.Compare and assume the output will be a T, when in fact you can only be sure it will be a Class1<U>.

    To provide a simpler, more comprehensible example, suppose I had this class:

    class Parser
    {
        public abstract object Parse(string text);
    }
    
    class Int32Parser
    {
        public int Parse(Parser parser, string text)
        {
            return parser.Parse(text);
        }
    }
    

    The above code makes a faulty assumption similar to your own: that parser.Parse will return an int just because int derives from object (just as in your case, T must derive from Class1<U>); in fact, you can only be sure it will return an object.

    There are two ways I can see to fix this problem: make Class1<T>.Compare a generic method:

    public abstract U Compare<U>(U otherObj) where U : Class1<T>;
    

    …or relax the type specificity of your Class3.Compare method’s return value:

    public Class1<U> Compare(T obj1, T obj2)
    {
        // ...
    }
    

    Personally, I would prefer the second unless you absolutely need the first. All these generic type constraints can become very messy and burden you more than you expect when the complexity starts to grow like this.

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

Sidebar

Related Questions

I have following classes. class A { public: void fun(); } class B: public
I have the following classes public interface InterfaceBase { } public class ImplementA:InterfaceBase {
I have the following classes: Ingredients, Recipe and RecipeContent... class Ingredient(models.Model): name = models.CharField(max_length=30,
I have the following classes: public class Person { public String FirstName { set;
I have the following ActiveRecord classes: class User < ActiveRecord::Base cattr_accessor :current_user has_many :batch_records
I have the following 3 classes Book Product SpecialOptions There are many Books, and
I have the following 3 rails classes, which are all stored in one table,
What is the best practice to implement the following: we have a classes Order
I have an Xcode project with the following group structure: ProjectName/ Classes/ class1.h class1.m
I have the following code inside a method: var list = new[] { new

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.