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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:17:59+00:00 2026-05-17T15:17:59+00:00

I have 2 variables of type object that needs to be compared using a

  • 0

I have 2 variables of type object that needs to be compared using a dynamic operation that is based on an enum:

public enum OperationType
{
    None,
    EqualTo,
    NotEqualTo,
    GreaterThan,
    GreaterThanOrEqualTo,
    LessThan,
    LessThanOrEqualTo          
}

You can make the assumption that the underlying type of the variables are the same, can only be of type string, or any other value type, but are unknown during development time.

I currently have the following:

 bool match;
 switch (Operation)
 {
      case OperationType.EqualTo:
           match = Equals(targetValue, valueToMatch);
           break;
      case OperationType.NotEqualTo:
           match = Equals(targetValue, valueToMatch) == false;
           break;
      case OperationType.GreaterThan:
           //??
           break;
      case OperationType.GreaterThanOrEqualTo:
           //??
           break;          
      case OperationType.LessThan:
           //??
           break;
      case OperationType.LessThanOrEqualTo:
           //??
           break;          
      default:
           throw new ArgumentOutOfRangeException();
 }

What is the best way to determine a match at runtime (C# 4.0) ?

  • 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-17T15:17:59+00:00Added an answer on May 17, 2026 at 3:17 pm

    Do you have this in a generic method, or could you put it in a generic method? If so, it’s relatively easy: use EqualityComparer<T>.Default for the equality comparisons, and Comparer<T>.Default for greater than / less than comparisons (using a < 0 or <= 0 for less-than vs less-than-or-equal-to for example).

    If you can’t call it directly in a generic way, you could use C# 4’s dynamic typing to do it for you:

    private static bool CompareImpl<T>(T value1, T value2, OperationType operation)
    {
        // Stuff using generics
    }
    
    public static bool Compare(dynamic value1, dynamic value2,
                               OperationType operation)
    {
        return CompareImpl(value1, value2, operation);
    }
    

    At execution time, that will work out the right type argument to use for CompareImpl.

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

Sidebar

Related Questions

I have an object array that is being passed and then split up into
(Please no advise that I should abstract X more and add another method to
I have been lately reading a lot on memory allocation schemes in java, and
I have been following the excellent book Programming Ground Up, wanting to learn assembly.
I would like to know if there's any templating engine that is used for
I'm rather new to both Java programming and to Android development, so my learning
I spend about 2 hours reading a lot of factory related topics and I
it may be a duplicate of How to dynamically call a class' method in
In a Django view you can access the request.GET['variablename'] , so in your view

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.