-1

This code will return true:

private bool SimpleComparison()
{
    return new byte() == new byte();
}

This code will return false:

private bool AnotherSimpleComparison()
{
    return new byte[0] == new byte[0];
}

Can't understand, why? As I understand, in the second case it is differend addresses? And what about first case?

Amazing User
  • 3,473
  • 10
  • 36
  • 75
  • 1
    All of the answers on the duplicate are wrong; this has nothing to do with whether or not the types in question are value types. – Servy Dec 08 '14 at 18:12

1 Answers1

3

There is an overload of the == operator in which both operands are of type byte and it is implemented to compare the value of each byte; in this case you have two zero bytes, and they are equal.

The == operator isn't overloaded for arrays, so the overload having two object operands is used in the second case, and its implementation compares the references to the two objects. The reference to the two arrays are different.

Habib
  • 219,104
  • 29
  • 407
  • 436
Servy
  • 202,030
  • 26
  • 332
  • 449