这是因为零整数可以隐式转换为枚举:
enum SqlDbType
{
Zero = 0,
One = 1
}
class TestClass
{
public TestClass(string s, object o)
{ System.Console.WriteLine("{0} => TestClass(object)", s); }
public TestClass(string s, SqlDbType e)
{ System.Console.WriteLine("{0} => TestClass(Enum SqlDbType)", s); }
}
// This is perfectly valid:
SqlDbType valid = 0;
// Whilst this is not:
SqlDbType ohNoYouDont = 1;
var a1 = new TestClass("0", 0);
// 0 => TestClass(Enum SqlDbType)
var a2 = new TestClass("1", 1);
// => 1 => TestClass(object)
(改编自Visual C# 2008 重大更改 - 更改 12 http://msdn.microsoft.com/en-us/library/cc713578.aspx#sectionToggle1)
当编译器执行重载决策时 0 是适用函数成员 http://msdn.microsoft.com/en-us/library/aa691337%28v=vs.71%29.aspx对于两个SqlDbType
和object
构造函数,因为:
存在从参数类型到相应形参类型的隐式转换(第 6.1 节)
(Both SqlDbType x = 0
and object x = 0
有效)
The SqlDbType
参数优于object
参数因为更好的转换规则 http://msdn.microsoft.com/en-us/library/aa691339%28v=vs.71%29.aspx:
- If
T1
and T2
are the same type, neither conversion is better.
-
object
and SqlDbType
不是同一类型
- If
S
is T1
, C1
is the better conversion.
- If
S
is T2
, C2
is the better conversion.
- If an implicit conversion from
T1
to T2
exists, and no implicit conversion from T2
to T1
exists, C1
is the better conversion.
- 没有隐式转换
object
to SqlDbType
exists
- If an implicit conversion from
T2
to T1
exists, and no implicit conversion from T1
to T2
exists, C2
is the better conversion.
- 隐式转换自
SqlDbType
to object
存在,所以SqlDbType
是更好的转换
注意什么exactly构成一个常数 0 已经(非常微妙地)改变了视觉 C# 2008 http://msdn.microsoft.com/en-us/library/cc713578.aspx#sectionToggle1(微软对 C# 规范的实现)正如 @Eric 在他的回答中解释的那样。