等于 int 和 short c# returns false

Equals int and short c# returns false

考虑这段代码:

static int x2 = 10;

public static void Main()
{
     short y = 10;
     Console.WriteLine(y.Equals(x2)); //False
     Console.Read();
}

为什么y.Equals(x2)returnsfalse

Int16.Equals具体docs

Return Value

true if obj is an instance of Int16 and equals the value of this instance; otherwise, false.


这是我的原始答案,虽然它不适用于此处,但我将其留在此处作为 .Equals 方法正在检查的内容的注释

来自docs,

the Equals(Object) method tests for reference equality

the documentation,你可以读到使用的特定重载:

Returns a value indicating whether this instance is equal to a specified object.

并且:

true if obj is an instance of System.Int16 and equals the value of this instance; otherwise, false.

A short 不是 int,所以它 returns false