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

The Archive Base Latest Questions

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

Consider this admittedly contrived Generic Definition: private void Foo<T,BASETYPE>(PropertyInfo prop, BASETYPE o1, BASETYPE o2)

  • 0

Consider this admittedly contrived Generic Definition:

    private void Foo<T,BASETYPE>(PropertyInfo prop, BASETYPE o1, BASETYPE o2)
    {
        T value1 = (T) prop.GetValue(o1, null);
        T value2 = (T) prop.GetValue(o2, null);
        if (value1 != value2)
            Console.WriteLine("NOT EQUAL");
    }

prop is guaranteed to be a PropertyInfo for BASETYPE.

I am getting a compile error at the if() statement:

Operator '!=' cannot be applied to operands of type 'T' and 'T'

While in the “general case” I understand that the error message is valid, in this case, I only want the routine for some of the standard types: System.Int64, System.String, etc all of which support the == and != operator.

I assume this can be fixed with a “where” clause, but IComparable and IEqualable don’t help.

Do anyone know what the correct “where” clause is?

Frank

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

    Since System.Int64, System.String, etc .. from your list implement IComparable, you could use

    where T : IComparable
    

    and use CompareTo() instead of !=

    For eg. this code would compile

    private void Foo<T>(object o) where T : IComparable
    {
        T v1 = default(T);
        T v2 = default(T);
        if(v1.CompareTo(v2)  != 0)
        {
            Console.WriteLine("Not Equal");
        }
    }
    
    private void Bar()
    {
        Foo<string>(new object());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this contrived, trivial example: var foo = new byte[] {246, 127}; var bar
Consider this class: public class Foo { // Fields private string _bar; // Properties
Consider this template function: template<typename ReturnT> ReturnT foo(const std::function<ReturnT ()>& fun) { return fun();
consider this simple function def foo(l=[]): if not l: print List is empty else
Consider this simple example - public class Person { private String name; private Date
Consider this DialogFragment: public class RollTriggerDialog extends DialogFragment{ private ProgressDialog _dialog; int _progress; public
Consider this situation: void doSmth1(std::map<int,int> const& m); void doSmth2(std::map<int,int> const& m) { std::map<int,int> m2
Consider this function template: template<typename T> unsigned long f(void *) { return 0;} Now,
Consider this #define : #define msd_buffer ((volatile char *) MSD_BUFFER_ADDRESS) and this variable definition:
Consider this data { _id : ..., array : [ { name : value1,flag

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.