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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:11:43+00:00 2026-05-26T11:11:43+00:00

How can I represent, in a class, something that represents a Value. This value

  • 0

How can I represent, in a class, something that represents a Value.

This value will be used to write comparison operations on, like EqualTo, GreaterThan etc.

Now the catch is, the value could sometimes be an integer, string, or even a list of integers or strings.

How could I represent this in a class or groups of classes (maybe inheriting from a base class)

Could I then write a single method like:

EqualTo(IValue value, IValue otherValue)

How could this EqualTo method handle now if I passed in a string, or a list of strings, datetime or a list of datetimes and return true/false accordingly?

Note:

The catch is I will be building up these objects from data that comes from the database, so the actual dataType, value or values etc.

  • 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-26T11:11:43+00:00Added an answer on May 26, 2026 at 11:11 am

    You could write:

    EqualTo<T>(T value1, T value2) where T : IEquatable<T>
    

    or for greater than and less than:

    Compare<T>(T value1, T value2) where T : IComparable<T>
    

    Both of those would be pretty simple, given IComparable<T> and IEquatable<T> 🙂

    You’d need separate overloads (or different methods) to handle a sequence of such values though:

    SequenceEqualTo<T>(IEnumerable<T> sequence1, IEnumerable<T> sequence2)
        where T : IEquatable<T>
    
    
    SequenceCompare<T>(IEnumerable<T> sequence1, IEnumerable<T> sequence2)
        where T : IComparable<T>
    

    Alternatively, if you don’t want the generic constraint, you could use Comparer<T>.Default and EqualityComparer<T>.Default.

    If you wanted to handle sequences in the same methods as non-sequences, you could always check whether T implemented IEquatable<T> and if not, whether it implemented IEnumerable<TElement> for some TElement which implemented IEquatable<TElement>. It would get pretty confusing, mind you…

    EDIT: Okay, if you’re going to be given these things dynamically…

    • First work out whether they’re lists or not. You’ll want to handle that separately. Assuming it really is List<T>, you could do:

      if (value1.GetType().IsGenericType &&
          value1.GetType().GetGenericTypeDefinition() == typeof(List<>))
      {
          // Handle this case separately, definitely in another method.
          // It'll be a pain.
      }
      
    • Otherwise, use just the normal Equals() call to check for equality, and cast a value to the non-generic IComparable type for greater-than/less-than. It’s not ideal, but it should work…

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

Sidebar

Related Questions

I was wondering if there is any library that can be used to represent
Say I have the value: -5 that represents a TimeZone (GMT-5). How can I
I'm trying to write a helper class that represents fields on an object. The
I'd like to create a structured type in Moose that can be used as
In ActionScript 3, there are some classes that will represent a value rather than
Let's say I have a date that I can represent in a culture-invariant format
I have an unordered tree. Each node represents a task that can be done
Given an XDocument instance, how can I easily get a TextReader that represents that
My Model is a generic class that contains a (for example) Value property which
I have an IEnumerable of an item class defined like this: public class Item

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.