C#: Casting '0' to int

后端 未结 3 1950
Happy的楠姐
Happy的楠姐 2020-12-20 13:29

I saw a code like this:

private readonly object[] m_Values = { (int)0, (int)0 };

What\'s the idea to cast 0 to int? Isn\'t it int by \'defa

3条回答
  •  温柔的废话
    2020-12-20 14:03

    It is not necessary. I'd assume the programmer got bitten before. I'll post it as a puzzler, which overload will be called in this program?

    using System;
    
    class Program {
        static void Main(string[] args) {
            Foo.Bar(0);
            Console.ReadLine();
        }
    }
    
    class Foo {
        public static void Bar(byte arg)  { Console.WriteLine("byte overload"); }
        public static void Bar(short arg) { Console.WriteLine("short overload"); }
        public static void Bar(long arg)  { Console.WriteLine("long overload"); }
    }
    

提交回复
热议问题