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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:18:20+00:00 2026-05-13T09:18:20+00:00

(C#, VS2008) In a program I’m working on, I’ve got lots of objects that

  • 0

(C#, VS2008) In a program I’m working on, I’ve got lots of objects that all have an ID and implement IComparable so that List<>-s of the various objects are easily searchable by ID. Since I hate copy/pasting code, I thought I’d abstract that bit of functionality down to a base class, like so:

using System;

namespace MyProg.Logic
{
    abstract class IDObject : IComparable<IDObject> 
    {
        private int miID;

        public int ID
        {
            get { return miID; }
            set { miID = value; }
        }

        public IDObject(int ID)
        {
            miID = ID;
        }

        #region IComparable<IDObject> Members

        int IComparable<IDObject>.CompareTo(IDObject other)
        {
            return miID.CompareTo(other.miID);
        }

        #endregion
    }
}

The drawback I see to that is that two separate classes that each inherit it would be directly comparable using .CompareTo() and I was hoping to enforce that each class that inherits from IDObject is only Comparable to others of the exact same class. So I was hoping to figure out how to do that and came up with this

using System;

namespace MyProg.Logic
{
    abstract class IDObject : IComparable<T> where T : IDObject
    {
        private int miID;

        public int ID
        {
            get { return miID; }
            set { miID = value; }
        }

        public IDObject(int ID)
        {
            miID = ID;
        }

        #region IComparable<T> Members

        int IComparable<T>.CompareTo(T other)
        {
            return miID.CompareTo(other.miID);
        }

        #endregion
    }
}

But that gives a compile error of “Constraints are not allowed on non-generic declarations”

Looking at it, I’m sure there’s a way to do something like that so that each class is only comparable to other instances of that same class, but I can’t tease out the syntax.

  • 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-13T09:18:20+00:00Added an answer on May 13, 2026 at 9:18 am

    You can use the Curiously Recurring Template Pattern to solve this problem.

    abstract class Base<T> : IComparable<T> where T : Base<T> {
        public int Rank { get; set; } // Order instances of derived type T by Rank
        public int CompareTo(T other) { return Rank.CompareTo(other.Rank); }
    }
    class Foo : Base<Foo> {}
    class Bar : Base<Bar> {}
    
    static class Program {
       static void Main() {
           var foo1 = new Foo { Rank = 1 };
           var foo2 = new Foo { Rank = 2 };
           var bar1 = new Bar { Rank = 1 };
           var bar2 = new Bar { Rank = 2 };
    
           Console.WriteLine(foo1.CompareTo(foo2));
           Console.WriteLine(bar2.CompareTo(bar1));
    
           //error CS1503: Argument '1': cannot convert from 'Bar' to 'Foo'
           //Console.WriteLine(foo1.CompareTo(bar1));
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.