Method overloading in subclass (Enum vs int)

Why do the following two code examples produce different output?

Case 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(); } } 

Conclusion:

 Func(int) Func(EnumType) 

Case 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(); } } 

Conclusion:

 Func(EnumType) Func(EnumType) 

I am puzzled. Does this mean that Func (EnumType) hides Func (int) declared in the database? If so, why does literally 0 implicitly rush into EnumType in the second case without warning?

EDIT:

When i try to execute

even more interesting behavior
 Console.WriteLine(b.Func(0)); Console.WriteLine(b.Func(1)); Console.WriteLine(b.Func(EnumType.First)); 

What do you think should look like?

there he is:

  Func(EnumType) Func(int) Func(EnumType) 

Any ideas why 0 and 1 are handled differently?

EDIT 2:

It turns out that the letter C really has special meaning in C #.

Here and here I found an excellent description of this behavior (see accepted answers).

+6
source share
2 answers

Yes, it hides Func (int) declared in class A.

Also see enum (C # link)

The main type of the default enumeration elements is int

You can also see Polymorphism (C # Programming Guide)

EDIT

If you want to change

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

to

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

you will see that the output should be

 Func(int) Func(int) Func(EnumType) 

It would seem that 0 is implicitly passed to the default enum value if it is passed directly to the function call.

EDIT 2

I checked the IL code and it seems to implicitly translate 0 to enum

 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 
+5
source

The main type of enumeration is int, so the ClassB function hides the version of ClassA.

Try changing it to this:

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

You should see that it calls the int function.

0
source

Source: https://habr.com/ru/post/920650/


All Articles