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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:55:53+00:00 2026-05-15T15:55:53+00:00

For classes, == and != uses object.ReferenceEquals. But for structs, == and != is

  • 0

For classes, == and != uses object.ReferenceEquals. But for structs, == and != is not defined.

struct S { }
S s1 = new S();
s1 is ValueType; // true
S s2 = new S();
object.Equals(s1, s2); // true
s1 == s2; // operator '==' cannot be applied.

The default behavior for ValueType equals is reflecting over all fields and checking equality, right..? So why isn’t == and != defined to just use object.Equals for structs?

Then I took a look at System.Int32 in Mono to see what they did.. Int32 derives from IFormattable, IConvertible, IComparable, IComparable<Int32>, IEquatable<Int32>, but it does not implement == and !=… But still, == and != can be used on integers as expected.

Is there some compiler magic happening on one of these interfaces or with the built-in valuetypes? Or am I missing something crucial here?

Edit: Btw, could the reason == isn’t implemented on structs be for performance reasons? Using reflection to iterate over all the fields might be a bit slow…?

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

    Comparison for System.Int32 are defined in the C# norm in 14.9.1 Integer comparison operators and are mapped directly to the IL opcodes like OpCode.Ceq for equality in the C# compiler so they aren’t defined as standard operators on the System.Int32 type directly.

    So yes compiler magic it is.

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

Sidebar

Related Questions

One of classes in my program uses some third-party library. Library object is a
MyClassWithMainMethod.java uses classes of someJar.jar. If I call: java -cp someJar.jar MyClassWithMainMethod I get
This tutorial on mobileorchard.com uses 2 classes (or 2 sets of .h and .m)
I'm developing a DLL that uses WPF classes to make image manipulation. It works
I have a window application which uses SP classes to create a site. I
I've written a Servlet that uses the library ganymed-ssh2-build210.jar (it uses these classes: import
I have multiple classes that will store similar data for different uses and I
I have rather large project that uses ICU regex classes. Basically it might run
Raising events in multithreaded classes? I am running a class(gamepad handler) that uses many
I have created a project that uses scala that has some abstract classes definitions

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.