Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 重载子类中的方法(Enum vs int)_C#_Overloading_Derived Class - Fatal编程技术网

C# 重载子类中的方法(Enum vs int)

C# 重载子类中的方法(Enum vs int),c#,overloading,derived-class,C#,Overloading,Derived Class,为什么以下两个代码示例产生不同的输出 案例1 enum EnumType { First, Second, Third } class ClassB { public string Func(int index) { return "Func(int)"; } public string Func(EnumType type) { return "Func(EnumType)"; } }

为什么以下两个代码示例产生不同的输出

案例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)
Func(EnumType)
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(int)
Func(EnumType)
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#中确实有特殊的意义

我发现了对这种行为的极好描述(请参阅公认的答案)。

是的,它确实隐藏了类A中声明的Func(int)

此外,请参阅

枚举元素的默认基础类型为int

你可能还想看看

编辑

如果你想改变

Console.WriteLine(b.Func(0));          
Console.WriteLine(b.Func(1)); 
Console.WriteLine(b.Func(EnumType.First)); 

您会发现输出应该是

Func(int)   
Func(int)   
Func(EnumType)
如果直接传递给函数调用,则0似乎隐式转换为默认枚举值

编辑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();
    }
}
我检查了IL代码,它似乎确实隐式地将0强制转换为枚举

IL_0000: nop
IL_0001: newobj instance void ClassB::.ctor()
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: ldc.i4.0
IL_0009: callvirt instance string ClassB::Func(valuetype EnumType)
IL_000e: call void [mscorlib]System.Console::WriteLine(string)
IL_0013: nop
IL_0014: ldloc.0
IL_0015: ldc.i4.0
IL_0016: callvirt instance string ClassB::Func(valuetype EnumType)
IL_001b: call void [mscorlib]System.Console::WriteLine(string)
IL_0020: nop
IL_0021: call string [mscorlib]System.Console::ReadLine()
IL_0026: pop
IL_0027: ret

枚举的基础类型是int,因此ClassB函数确实隐藏了ClassA版本

尝试将其更改为:

enum EnumType : byte
{
    First,
    Second,
    Third
}
...
Console.WriteLine(b.Func(256));  // out of range for a byte

您应该看到它调用了int函数。

为什么从int(0)到EnumType的隐式转换没有警告?您可能想阅读更多关于隐式转换@atander的内容:0和1之间仍然存在“神奇”的区别(请参见我编辑的问题)。是否有文档记录?如果直接传递给函数调用,0似乎隐式转换为默认枚举值。我倾向于这种解释,并接受您的答案。谢谢