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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:18:55+00:00 2026-05-19T00:18:55+00:00

I have the following base abstract class defined as: public abstract class BaseObject<T> :

  • 0

I have the following base abstract class defined as:

public abstract class BaseObject<T> : IComparable, IComparable<T>, IEquatable<T> {}

I also have an interface defined as:

public interface ICode<T> where T : struct
{
    T Code { get; }
}

Now I want to derive a class that is inherited from BaseObject<T> and includes interface ICode<T>.

I tried to define it like that:

public class DerivedObject<T, U> : BaseObject<T>, ICode<U> where T : DerivedObject<T, U> where U : struct
{

    public DerivedObject(U code)
    {
        Code = code;
    }

    // From BaseObject
    protected override int InstanceCompareTo(T obj)
    {
        return Code.CompareTo(obj.Code);
    }

    // From BaseObject
    protected override bool InstanceEquals(T obj)
    {
        return Code.Equals(obj.Code);
    }

    // From ICode
    U _Code;

    public U Code 
    { 
        get { return _Code; }
        protected set { _Code = value; }
    }
}

The only error that comes from the compiler is for Code.CompareTo(obj.Code) with the message:

‘U’ does not contain a definition for ‘CompareTo’ and no extension method ‘CompareTo’ accepting a first argument of type ‘U’ could be found.

But U is a value type and should know CompareTo.

Have you any idea what I am doing wrong, or if I do all wrong?

My final aim is to derive classes such these:

    public class Account : DerivedObject<Account, int>
    public class ItemGroup : DerivedObject<ItemGroup, string>

Big Thanks In Advance!

  • 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-19T00:18:56+00:00Added an answer on May 19, 2026 at 12:18 am

    It is not necessary for a struct type to have a CompareTo method. You have to enforce it using constraint:

    public class DerivedObject<T, U> : BaseObject<T>, ICode<U>
        where T : DerivedObject<T, U>
        where U : struct, IComparable<U> {}
    

    CompareTo method is part of IComparable<T> contract. And it has nothing to do with U being a value type. That is why, you have to add a generic constraint that states U is IComparable<U>.

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

Sidebar

Related Questions

I have the following code internal abstract class Base { public DateTime Time; public
I have an abstract entity base class defined like this: public abstract class SessionItem
I have the following base class. public abstract class BaseEntity { public virtual long
I have the following structure: abstract class Base { public abstract List<...> Get(); //What
I have this base class having the following interface: abstract class Base { abstract
Assuming I have the following classes in an application: A base class: public abstract
I have the following base class: abstract class Base { public abstract object Var
For simplicity's sake, lets say I have the following Abstract Base Controller Class: public
Suppose I have the following (trivially simple) base class: public class Simple { public
I have the following class hierarchy [BsonKnownTypes(typeof(MoveCommand))] public abstract class Command : ICommand {

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.