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 (since arrays are of type object
) in the second case, and its implementation compares the references to the two objects. The reference to the two arrays are different.
It's worth noting that this has nothing (directly) to do with the fact that byte
is a value type and arrays are reference types. The ==
operator for byte
has value semantics only because there is a specific overload of the operator with that implementation. If that overload did not exist then there would be no overload for which two bytes would be valid operands, and as such the code wouldn't compile at all. You can see this easily enough by creating a custom struct
and comparing two instances of it with the ==
operator. The code will not compile, unless you provide your own implementation of ==
for those types.