1

How does this work when Int32 does not implement an equality operator?

 bool y = 6 == 5; 
1
  • Ok I asked a leading question here when I knew the answer. I really should have asked the specific question. So here is it stackoverflow.com/questions/9552760/… Commented Mar 4, 2012 at 6:11

3 Answers 3

6

The == operator for primitive numeric types is part of the language spec, not the framework types.
It compiles to a dedicated IL instruction (ceq), not a method call.

The same is true for all other primitive operators, including arithmetic, comparison, and conversions.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Give a Type how would you detect that a type supports ceq?
@Simon: You wouldn't. That's a language-level feature. What are you trying to do?
2

How does this work when Int32 does not implement an equality operator?

Excuse me, I think your flawed premise is showing :-) From the docs:

The Int32 type supports standard mathematical operations such as addition, subtraction, division, multiplication, subtraction, negation, and unary negation. Like the other integral types, the Int32 type also supports the bitwise AND, OR, XOR, left shift, and right shift operators.

You can use the standard numeric operators to compare two Int32 values, or you can call the CompareTo or Equals method.

Comments

0

Just to add to SLaks answer, comparing int32 is something that is done so often by so many classes that it would be a huge performance issue not to have this implemented in a custom way. This dedicated IL instruction will actually compile to a machine instruction that is extremely fast to perform this operation.

Its the same trade off that java had to make with it's primitive types. With an OO language you always have the question of "Purity" where you don't have an int32, you have a "Integer" object or you allow primitive types, accepting the complexity and benefiting from the speed that they can add.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.