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).
source share