我看到这样的代码:
private readonly object[] m_Values = { (int)0, (int)0 };
Run Code Online (Sandbox Code Playgroud)
将0转换为int的想法是什么?不是'默认'的int吗?
Han*_*ant 17
没有必要.我假设程序员之前被咬过了.我会把它作为一个益智游戏发布,在这个程序中会调用哪个超载?
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"); }
}
Run Code Online (Sandbox Code Playgroud)
我认为这样做是没有意义的,但我认为唯一有用的地方是原始编码器希望阻止将该值转换为其他数据类型的地方.考虑这个例子:
object[] m_Values = { (int)0, (int)0 };
Int16 item = (Int16) m_Values[0];
Run Code Online (Sandbox Code Playgroud)
要么
object[] m_Values = { (int)0, (int)0 };
Int64 item = (Int64)m_Values[0];
Run Code Online (Sandbox Code Playgroud)
以上将导致
指定演员表无效.
但是下面会有效:
object[] m_Values = { (int)0, (int)0 };
int item = (int)m_Values[0];
Run Code Online (Sandbox Code Playgroud)