0

Consider following example:

public class Program
{
    public enum ExampleEnum
    {
        Value1,
        Value2
    }

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

        Int32 value = 0;

        OverloadedMethod(value);
    }

    private static void OverloadedMethod(ExampleEnum arg)
    {
        Console.WriteLine("Overload with enum");
    }

    private static void OverloadedMethod(object arg)
    {
        Console.WriteLine("Overload with object");
    }
}

Program outputs are: "Overload with enum", "Overload with object". Why is it so? How exactly does C# compiler interprets direct values? Side note: output is the same when zero is explicitly casted to Int32:

    public static void Main(string[] args)
    {
        OverloadedMethod((Int32)0);

        Int32 value = 0;

        OverloadedMethod(value);
    }
Artur Gajewski
  • 111
  • 1
  • 8
  • 1
    Because an enum member of 0 is a special case, and enums have integral types as underlying value. – CodeCaster May 15 '18 at 08:49
  • 2
    [Implicit enumeration conversions](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/conversions#implicit-enumeration-conversions): "An implicit enumeration conversion permits the *decimal_integer_literal* 0 to be converted to any *enum_type* ..." – Damien_The_Unbeliever May 15 '18 at 08:49
  • Thanks! I did not realize that the value of argument is responsible for such behavior. – Artur Gajewski May 15 '18 at 08:56

0 Answers0