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