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 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

Let's say I have defined the following class: public abstract class Event { public
I have the following structure: class Base { } class Child : Base {
Suppose we have the following class hierarchy: class Base { ... }; class Derived1
I have the following models. # app/models/domain/domain_object.rb class Domain::DomainObject < ActiveRecord::Base has_many :links_from, :class_name
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I want to do the following public abstract class MyAbstractClass { public static abstract
I have following situation: I have loged user, standard authentication with DB table $authAdapter
I have following string String str = replace :) :) with some other string;
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
I have following situation. A main table and many other tables linked together with

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.