|
-
June 30th, 2010, 04:36 AM
#1
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).
-
June 30th, 2010, 12:57 PM
#2
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.
Last edited by [email protected]; June 30th, 2010 at 01:02 PM.
-
June 30th, 2010, 02:38 PM
#3
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.
-
June 30th, 2010, 02:51 PM
#4
Re: Strange function overloading behavior with int and enum
 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)
Last edited by [email protected]; June 30th, 2010 at 03:13 PM.
-
June 30th, 2010, 03:16 PM
#5
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.
-
June 30th, 2010, 05:35 PM
#6
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
-
June 30th, 2010, 05:40 PM
#7
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.
--------------------------------------------------------------------------------------------------------------------------
Disclaimer - Most likely any code I have posted as an answer was most likely written free hand and may have some minor compile errors, and is merely intended to give you the idea.
-
June 30th, 2010, 05:41 PM
#8
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.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|