这是一小段代码,调用它时会输出“double”。为什么?这背后的原因是什么。为什么它不打印“ float ”?
class source
{
static void Main()
{
Receiver r = new Receiver();
r.Method1(1.1);
}
}
class Receiver
{
public virtual void Method1(double f) { Debug.Print("double"); }
public virtual void Method1(float f) { Debug.Print("float"); }
}
TIA
最佳答案
要像这样指定 float 调用:
r.Method1(1.1f);
否则它会默认为双倍,就像您观察到的那样。
这是 MSDN documentation 的一部分双重解释了原因:
By default, a real numeric literal on the right-hand side of the assignment operator is treated as double.
关于c# - 在 C# 中调用时执行 Double & Float 方法参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1557234/