1

Equality operator (==) is a reference type and we all know that Int32 is a struct which means that is a value type. I also checked the inside of the Int32 and I couldn't find any Operator Overloading related to Equality Operator (==).

So, my question is why can we apply == on Int32?

I also checked the decimal type and I noticed that it has some Operator Overloading, so naturally we can use == on decimal types.

enter image description here

Alireza Ahmadi
  • 8,579
  • 5
  • 15
  • 42
  • "Equality operator (==) is a reference type" What? Where did you get the idea that `==` is only to be used for reference types? – Kirk Woll May 22 '22 at 15:58
  • Here is the relevant docs page: [https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/equality-operators](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/equality-operators) – SpaceBeeGaming May 22 '22 at 16:01
  • @KirkWoll I have learned that `==` check reference of objects and the `Equals()` method compares only content. Is it incorrect? – Alireza Ahmadi May 22 '22 at 16:01
  • @AlirezaAhmadi it is true that `==` often compares reference equality (for reference types), unless `==` is overridden. It also compares value equality for value types (naturally for built-in primitives, and via operator overloading for other value types). For reference types, by default, `Equals` does reference comparison. However, it can be overridden to provide a comparison of the object's contents. – Kirk Woll May 22 '22 at 16:03
  • The `==` operator for `int` as well as all other operators for all the primitive types, are implemented as compiler intrinsics, so you won't find the source code. But they do a value comparison, there is no boxing – Charlieface May 22 '22 at 16:06
  • I think the mistake you are making is thinking that looking at the source code for primitive types tells you the whole story. It doesn't. Things like adding, equality, and other operations on primitives are handled at the bytecode layer and within the CLR runtime. – Kirk Woll May 22 '22 at 16:06
  • Related: https://stackoverflow.com/questions/23204925/why-is-overridden-in-system-double-but-not-in-system-int32-and-what-are-the-r – Klaus Gütter May 22 '22 at 16:10
  • @KirkWoll Thanks. So why `Double` data type needs to use `Operator Overloading` for `==`. because `Double` is a primitive type – Alireza Ahmadi May 22 '22 at 16:10
  • 1
    @AlirezaAhmadi that's a reasonable question. Perhaps you could refine your question to focus on that. – Kirk Woll May 22 '22 at 16:15
  • By default `==` will compare the value of the variables. For reference types that would be the reference, but for value types that's the actual value. That's why you only need to override it for reference types when you want value equality or when you want to do something special for value types equality. – juharr May 22 '22 at 16:18
  • @AlirezaAhmadi `==` can be applied to compare values types, since a reference by itself is a value it compares the value of the reference itself and not the object that is referred – YaRmgl May 22 '22 at 16:30

1 Answers1

1

Per the equality operators documentation for value types:

Operands of the built-in value types are equal if their values are equal

Also, be aware that

User-defined struct types don't support the == operator by default. To support the == operator, a user-defined struct must overload it.

So, in a nutshell all user-defined structs must define the equality operator to overload it. But all built-in value types don't need to, it "just works".

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • Thanks. As you said `all built-in value types don't need to, it`. So why `Double` data type, which is a built-in value type, needs to use `Operator Overloading` for `==` – Alireza Ahmadi May 22 '22 at 18:01
  • 1
    Floating point types are a special case because of NaN and infinity. That being said, it is [discouraged to use equality on floating point types without using an epsilon value](https://stackoverflow.com/a/3875619). Rounding errors can make numbers that are approximately equal (and even display equally) unequal. – NightOwl888 May 22 '22 at 18:26