调用int和float以及double方法的程序 [英] program which calls int and float and double methods
问题描述
如果我打电话给add(1.1);我收到编译错误 - '最佳重载方法不匹配'。但我在代码中有一个float方法。为什么我仍然得到这个错误?示例代码如下,
If i call add(1.1); i am getting a compilation error - 'best overload method doesnot match'. But i have a float method in the code. why still i am getting this error ?? Sample code is given below,
private void button1_Click(object sender, EventArgs e)
{
add(0.1);
}
void add(int i)
{
MessageBox.Show("int i");
}
void add(float i)
{
MessageBox.Show("float i");
}
如果我打电话给add(1);我会得到答案'int i'是正确的。
如果我打电话给add(1.1);我收到编译错误 - '最佳重载方法不匹配'。但我在代码中有一个float方法。为什么我仍然得到这个错误?
我写了一个有'双'参数编译错误的方法。
If i call add(1); i will get the answer 'int i' which is right.
If i call add(1.1); i am getting a compilation error - 'best overload method doesnot match'. But i have a float method in the code. why still i am getting this error ??
I f i write a method which has a 'double' argument compilation error goes.
推荐答案
1.1
被解释为double
,而不是float
。您需要为float添加后缀 f (1.1f
)。此外,double
有可选后缀 d 。
以下是控制台应用程序中的等效示例:
Hi,
1.1
is interpreted as adouble
, not as afloat
. You need to add suffix f for float (1.1f
). Also,double
has optional suffix d.
Here's equivalent example in a console application:
class Program
{
static void Main(string[] args)
{
add(1); // int
add(1.1f); // float
add(1.1); // double
Console.ReadLine();
}
static void add(int i)
{
Console.WriteLine("int i");
}
static void add(float i)
{
Console.WriteLine("float i");
}
static void add(double i)
{
Console.WriteLine("double i");
}
}
结果:
Result:
int i<br />
float i<br />
double i
这篇关于调用int和float以及double方法的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!