Equals int and short c# returns false [duplicate]

岁酱吖の 提交于 2019-12-10 18:01:28

问题


Consider this code:

static int x2 = 10;

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

Why y.Equals(x2) returns false?


回答1:


Int16.Equals specific docs

Return Value

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


This was my original answer, whilst it doesn't apply here, I've left it in as a note for what the .Equals method is checking for

From the docs,

the Equals(Object) method tests for reference equality




回答2:


From the documentation, you can read that the specific overload used:

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

And:

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

A short is not an int, so it returns false.



来源:https://stackoverflow.com/questions/33362737/equals-int-and-short-c-sharp-returns-false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!