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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:29:47+00:00 2026-06-11T17:29:47+00:00

Update 1 I realised that the original restriction of a simple generic type meant

  • 0

Update 1

I realised that the original restriction of a simple generic type meant that I was unable to compare objects of different kinds, regardless of whether they implemented IComparable<T> or not, as a result the latest is now:

public static bool IsLessThan<TSource, TComparer>(this TSource source, TComparer comparer)
    where TSource : IComparable<TComparer>
    where TComparer : IComparable<TComparer>
{
    return source.CompareTo(comparer) < 0;
}

Original

I’ve written a simple IsLessThan(IComparable comparer) extension method on IComparable interface. I’ve ran into a small problem however; I’ve realised that essentially it allows for any IComparable to be compared, which I’d rather not have. Alternatively I’m wondering if it’s possible to restrict the parameter using a generic type? Currently my extension method looks like this:

public static bool IsLessThan(this IComparable source, IComparable comparer) 
{
    return source.CompareTo(comparer) < 0;
}

Is there any way to use generics to ensure that source and comparer are the same type, whilst still maintaining the constraint of IComparable?

Example

int test = 2;
var resultOne = test.IsLessThan(3); // should return true
var resultTwo = test.IsLessThan("Hello world"); // shouldn't compile
  • 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-11T17:29:48+00:00Added an answer on June 11, 2026 at 5:29 pm

    Well, you could use:

    public static bool IsLessThan<T>(this T source, T comparer) where T : IComparable
    

    Or you could make it more constrained, using IComparable<T>:

    public static bool IsLessThan<T>(this T source, T comparer)
        where T : IComparable<T>
    

    The latter would also be more efficient in terms of avoiding boxing.

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

Sidebar

Related Questions

UPDATE: The initial idea was to nest the objects, but I then realised that
I am writing a simple script to update a table data. I am unable
UPDATE: I've realized that the below problem only occurs if the user has already
UPDATE UPI_ATTRIBUTE SET SITE_INC ='0' WHERE USER_PROFILING_NAME IN ('CAR_IMPLICIT','CAR_EXPLICIT') Above is my query that
I have written a simple test case that follows Apple's documentation and I am
I have recently realised that all of my media files that have special chars
I have realised that mnesia doesnot support auto-increment feature as does MySQL or other
I'm working on a site with a client they've just realised that they've given
i was carrying on building my site today and i realised that everytime i
I need to update a datastore entity in a way that will not be

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.