Both checks are different. The first one checks for identity, the second one for equality. In general two terms are identical, if they are refering to the same object. This implies that they are equal. Two terms are equal, if their values is the same.
In terms of programming identity is usually messured by reference equality. If the pointer to both terms are equal (!), the object they are pointing at is exactly the same one. However, if the pointers are different, the value of the objects they are pointing at can still be equal. In C# identity can be checked using the static Object.ReferenceEquals
member, whilst equality is checked using the non-static Object.Equals
member. Since you are casting two integers to objects (which is called "boxing", btw), the operatior ==
of object
performs the first check, which is by default mapped to Object.ReferenceEquals
and checks for identity. If you explicity call the non-static Equals
-member, dynamic dispatch results in a call to Int32.Equals
, which checks for equality.
Both concepts are similar, but not the same. They may seem confusing for first, but the small difference is very important! Imagine two persons, namely "Alice" and "Bob". They both are living in a yellow house. Based on the assumption, that Alice and Bob are living in a district, where houses are only differ in their color, they could both live in different yellow houses. If you compare both homes you will recognize, that they are absolutely the same, because they are both yellow! However, they are not sharing the same home and thus their houses are equal, but not identical. Identity would imply that they are living in the same house.
Note: some languages are defining the ===
operator to check for identity.