Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can anyone explain this interesting C# problem?

Tags:

c#

I hit a bizarre problem earlier which I have replicated in a new console application. I wonder, can anyone explain why this occurs?

    static void Main(string[] args)
    {
        DoSomething(0);

        Console.Read();
    }

    public static void DoSomething(int? value)
    {
        Console.WriteLine("Do Something int? called");
    }

    public static void DoSomething(MyEnum value)
    {
        Console.WriteLine("Do Something MyEnum called");
    }

    public static enum MyEnum : int
    {
        test

    }
}

You get an error on the DoSomething line:

Error 1 The call is ambiguous between the following methods or properties: 'DoSomething(int?)' and 'DoSomething(MyEnum)'

However if you change the zero to any other number, there is no such problem.

like image 892
NibblyPig Avatar asked Nov 26 '10 11:11

NibblyPig


People also ask

What is interesting about C?

C is the only programming language that exists for such a long period and still it is widely used. C is the basis of many other programming languages like C++, Java, JavaScript, Go, C#, PHP, Python, Perl, C-shell and many more.

Can you explain C language?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

What makes C so special?

C is a statically typed programming language, which gives it an edge over other dynamic languages. Also, unlike Java and Python, which are interpreter-based, C is a compiler-based program. This makes the compilation and execution of codes faster.


2 Answers

The C# language specification states that there is an implicit conversion from the integer literal 0 to any enum type:

13.1.3 Implicit enumeration conversions

An implicit enumeration conversion permits the decimal-integer-literal 0 to be converted to any enum-type.

Therefore, any other integer literal can only convert to int? and is thus unambiguous; but the literal 0 is convertible to both int? and your enum.

It doesn’t matter which values are defined in your enum. Even if you have an enum like enum Test { Cats=7 }, the above still applies. Remember that all enums can have all values of their underlying integer types and are not restricted to the values actually declared.

like image 118
Timwi Avatar answered Oct 07 '22 04:10

Timwi


This is because the literal 0 is implicitly convertible to any enum.

So in the first situation, the 0 is "equally" convertible to either int? or MyEnum. Neither conversion is "better" than the other so the compiler doesn't know which method you're expecting to call.

If you change the 0 to a 1 then it works because 1 is not implicitly convertible to MyEnum, so the only matching method is the one that takes an int? argument.

like image 23
LukeH Avatar answered Oct 07 '22 06:10

LukeH