Programmer Question
I'm wrestling with a weird, at least for me, method overloading resolution of .net. I've written a small sample to reproduce the issue:
class Program
{
static void Main(string[] args)
{
var test = new OverloadTest();
test.Execute(0);
test.Execute(1);
Console.ReadLine();
}
}
public class OverloadTest
{
public void Execute(object value)
{
Console.WriteLine("object overload: {0}", value);
}
public void Execute(MyEnum value)
{
Console.WriteLine("enum overload: {0}", value);
}
}
public enum MyEnum
{
First = 1, Second = 2, Third = 3
}
Will print:
enum overload: 0
object overload: 1
Basically the overload called is different depending on the value (0, 1) instead of the given data type.
Could someone explain?
Update
I should have pointed out that there's a different behaviour between C# 2 and C# 3
Do((long)0) => object overload //C# 2
Do((long)0) => enum overload //C# 3
Find the answer here
No comments:
Post a Comment