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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:18:30+00:00 2026-05-10T23:18:30+00:00

According to the documentation of the == operator in MSDN , For predefined value

  • 0

According to the documentation of the == operator in MSDN,

For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise. For reference types other than string, == returns true if its two operands refer to the same object. For the string type, == compares the values of the strings. User-defined value types can overload the == operator (see operator). So can user-defined reference types, although by default == behaves as described above for both predefined and user-defined reference types.

So why does this code snippet fail to compile?

bool Compare<T>(T x, T y) { return x == y; } 

I get the error Operator ‘==’ cannot be applied to operands of type ‘T’ and ‘T’. I wonder why, since as far as I understand the == operator is predefined for all types?

Edit: Thanks, everybody. I didn’t notice at first that the statement was about reference types only. I also thought that bit-by-bit comparison is provided for all value types, which I now know is not correct.

But, in case I’m using a reference type, would the == operator use the predefined reference comparison, or would it use the overloaded version of the operator if a type defined one?

Edit 2: Through trial and error, we learned that the == operator will use the predefined reference comparison when using an unrestricted generic type. Actually, the compiler will use the best method it can find for the restricted type argument, but will look no further. For example, the code below will always print true, even when Test.test<B>(new B(), new B()) is called:

class A { public static bool operator==(A x, A y) { return true; } } class B : A { public static bool operator==(B x, B y) { return false; } } class Test { void test<T>(T a, T b) where T : A { Console.WriteLine(a == b); } } 
  • 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. 2026-05-10T23:18:31+00:00Added an answer on May 10, 2026 at 11:18 pm

    ‘…by default == behaves as described above for both predefined and user-defined reference types.’

    Type T is not necessarily a reference type, so the compiler can’t make that assumption.

    However, this will compile because it is more explicit:

        bool Compare<T>(T x, T y) where T : class     {         return x == y;     } 

    Follow up to additional question, ‘But, in case I’m using a reference type, would the the == operator use the predefined reference comparison, or would it use the overloaded version of the operator if a type defined one?’

    I would have thought that == on the Generics would use the overloaded version, but the following test demonstrates otherwise. Interesting… I’d love to know why! If someone knows please share.

    namespace TestProject {  class Program  {     static void Main(string[] args)     {         Test a = new Test();         Test b = new Test();          Console.WriteLine('Inline:');         bool x = a == b;         Console.WriteLine('Generic:');         Compare<Test>(a, b);      }       static bool Compare<T>(T x, T y) where T : class     {         return x == y;     }  }   class Test  {     public static bool operator ==(Test a, Test b)     {         Console.WriteLine('Overloaded == called');         return a.Equals(b);     }      public static bool operator !=(Test a, Test b)     {         Console.WriteLine('Overloaded != called');         return a.Equals(b);     }   } } 

    Output

    Inline: Overloaded == called

    Generic:

    Press any key to continue . . .

    Follow Up 2

    I do want to point out that changing my compare method to

        static bool Compare<T>(T x, T y) where T : Test     {         return x == y;     } 

    causes the overloaded == operator to be called. I guess without specifying the type (as a where), the compiler can’t infer that it should use the overloaded operator… though I’d think that it would have enough information to make that decision even without specifying the type.

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

Sidebar

Ask A Question

Stats

  • Questions 112k
  • Answers 112k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think the answer is an emphatic "yes". Spring is… May 11, 2026 at 9:55 pm
  • Editorial Team
    Editorial Team added an answer All your <select>s must have the same name plus [n],… May 11, 2026 at 9:55 pm
  • Editorial Team
    Editorial Team added an answer I would recommend writing a web service that exposes the… May 11, 2026 at 9:55 pm

Related Questions

Imagine I have the following: private IEnumerable MyFunc(parameter a) { using(MyDataContext dc = new
One of the features of the Flash app I'm working on is to be
What exactly do *args and **kwargs mean? According to the Python documentation, from what
I have a WPF app which snaps to screen edges (I just set the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.