为什么以下两个代码示例产生不同的输出?重载子类中的方法(Enum vs int)
案例1
enum EnumType
{
First,
Second,
Third
}
class ClassB
{
public string Func(int index)
{
return "Func(int)";
}
public string Func(EnumType type)
{
return "Func(EnumType)";
}
}
class Program
{
static void Main(string[] args)
{
ClassB b = new ClassB();
Console.WriteLine(b.Func(0));
Console.WriteLine(b.Func(EnumType.First));
Console.ReadLine();
}
}
输出:
Func(int)
Func(EnumType)
案例2
enum EnumType
{
First,
Second,
Third
}
class ClassA
{
public string Func(int index)
{
return "Func(int)";
}
}
class ClassB : ClassA
{
public string Func(EnumType enumType)
{
return "Func(EnumType)";
}
}
class Program
{
static void Main(string[] args)
{
ClassB b = new ClassB();
Console.WriteLine(b.Func(0));
Console.WriteLine(b.Func(EnumType.First));
Console.ReadLine();
}
}
输出:
Func(EnumType)
Func(EnumType)
我很困惑。这是否意味着Func(EnumType)隐藏了在基础中声明的Func(int)?如果是这种情况,那么为什么立即0是含蓄地铸造到EnumType在第二种情况下没有警告?
编辑:
甚至还有当我尝试
Console.WriteLine(b.Func(0));
Console.WriteLine(b.Func(1));
Console.WriteLine(b.Func(EnumType.First));
什么是你的猜测输出看起来应该更有趣的行为?
那就是:
Func(EnumType)
Func(int)
Func(EnumType)
为什么0和1的区别对待任何想法?
编辑2:
事实证明,字面0的确在C#中的特殊含义。
Here和here我发现了这种行为的一个很好的描述(请参阅接受的答案)。
为什么没有关于从int(0)到EnumType的隐式转换的警告? – alexm 2012-07-17 04:41:01
您可能想要阅读更多关于隐式转换的内容http://msdn.microsoft.com/zh-cn/library/aa691280(v=vs.71) – 2012-07-17 04:54:22
@atander:0和1之间仍然存在“魔术”差异(见我编辑的问题)。它是否记录在某处? – alexm 2012-07-17 05:01:19