ValueType and operator!=

Krzysztof Żelechowski 21 Reputation points
2021-09-21T08:54:58.07+00:00

I have the following result:
((System.ValueType) 0) != (System.ValueType) 0 && ((System.ValueType) 0) .Equals (0)
The former is a bit unexpected. What should I make of it?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,281 questions
{count} votes

Accepted answer
  1. Viorel 112.5K Reputation points
    2021-09-21T16:14:40.483+00:00

    I think that since ValueType is a class, your code creates three different objects (instances). Although, the content of these instances is similar.

    Calling ‘!=’ on different objects (references) gives True. (It does not compare the contents of the objects in this case).

    Calling Equals returns True because it is designed to compare the contents.

    See also: “Boxing”.

    0 comments No comments

0 additional answers

Sort by: Most helpful