Strange function overloading behavior with int and enum
Hi,
I have a very strange problem with function overloading in C# when there are two possible overloads: one with enum, one with object as parameter. When i call the method with parameter 0 - the enum version is called. When I choose any other value - the object version is called. The short code snippet explains the problem:
Code:
class Program
{
enum Bar
{
Value1,
Value2,
Value3
}
static void Main(string[] args)
{
Foo(0);
Foo(1);
Console.ReadLine();
}
static void Foo(object a)
{
Console.WriteLine("object");
}
static void Foo(Bar a)
{
Console.WriteLine("enum");
}
}
Could anyone explain this strange behavior?
This is not just a theoretical or academical problem - I came across it when adding parameters to commands in Npgsql v1.0. This sort of language construct is used there and it causes my code do behave improperly (0 is added as NULL, because invalid overload of NpgsqlParametersCollection.Add() is used).
Re: Strange function overloading behavior with int and enum
I have to admit when I first read this I thought you were smoking crack. Obviously the solution to your example is an Explicit cast, but I get your point. I have absolutly no idea why it behaves this way. I even modified the enum and assigned different values 10,20,30 so that there was no 0 "Bar" value and it still calls the Foo(Bar a) when 0 is used as a argument.
Long and short of it, pretty darn interesting, I bet that was a b-word to debug.
Re: Strange function overloading behavior with int and enum
enum derives implicitly from int, so of course the int version will be called instead of the object version because it is more restrictive.
Re: Strange function overloading behavior with int and enum
Quote:
Originally Posted by
BigEd781
enum derives implicitly from int, so of course the int version will be called instead of the object version because it is more restrictive.
I can argue why it would call foo(Bar) or foo(object) with equal enthusiasm, but what does not make any sense is why It calls foo(bar) when the calling foo(0) and then calls foo(object) with foo(1)
Re: Strange function overloading behavior with int and enum
Oh... I must have missed that. Yeah, that is really odd. My first thought would be that there is no enum value that corresponds to 1, but I see you have already tested that. Very strange, this would be a great question for StackOverflow seeing as a few of the C# design team members (including Eric Lippert) frequent that forum.
Re: Strange function overloading behavior with int and enum
I posted the question to stackoverflow and got some interesting answers about language specifications and compiler bugs, if you're curious check it out
http://stackoverflow.com/questions/3...ding-and-enums
Re: Strange function overloading behavior with int and enum
Wow learn something new everyday. Thanks for the update, Im going to file that one away in a safe spot, because that would suck to try and debug that. I would just assume it would always call the object one and it would take me a long freaking time to question that assumption.
Re: Strange function overloading behavior with int and enum
This is a bug in the runtime unfortunately. The int32 '0' is implicitly compatible with an enum no matter what it's base type and that will be perferred over 'object'. This does not happen with floats or doubles.