Given the following overloaded methods:
public string Test(long item)
{
return \"Test with a long was called!\";
}
public string Test(int item)
{
re
The C# specification rules mean that the compiler prefers converting a short
to int
, not to object
. I think this is due to the following rule from 7.5.3.5 Better conversion target (link is to C# 5 spec download, or see equivalent from C# 1.2 online)
Given two different types T1 and T2, T1 is a better conversion target than T2 if at least one of the following holds:
- An implicit conversion from T1 to T2 exists, and no implicit conversion from T2 to T1 exists
- T1 is a signed integral type and T2 is an unsigned integral type. [other content omitted]
To rewrite it for this scenario, since an implicit conversion from int
to object
exists, and no implicit conversion from object
to int
exists, converting to int
is the better conversion.