gpt4 book ai didi

c# - 在 C# 中调用时执行 Double & Float 方法参数

转载 作者:太空宇宙 更新时间:2023-11-03 17:11:48 30 4
gpt4 key购买 nike

这是一小段代码,调用它时会输出“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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com