C# 参数接受错误的类型并仍正常运行—;为什么?
下面我有一个方法,我从互联网上搜索来计算C#中Excel的percentrank函数。我修改了一点,以适应我的程序,但没有改变主要的逻辑 该程序编译和运行良好,没有任何错误(我知道)。然而,在我的主要代码中,进一步检查代码后,我使用C# 参数接受错误的类型并仍正常运行—;为什么?,c#,methods,parameters,C#,Methods,Parameters,下面我有一个方法,我从互联网上搜索来计算C#中Excel的percentrank函数。我修改了一点,以适应我的程序,但没有改变主要的逻辑 该程序编译和运行良好,没有任何错误(我知道)。然而,在我的主要代码中,进一步检查代码后,我使用 double result = percentRank( array, x); 在哪里 x是一个整数 数组是一个列表(int) 它的类型与指定采用的percentRank方法不同,但仍然运行良好。我的问题是为什么 private
double result = percentRank( array, x);
在哪里
x是一个整数数组是一个列表(int) 它的类型与指定采用的percentRank方法不同,但仍然运行良好。我的问题是为什么
private static double percentRank(List<int> array, double x)
{
// Calculate the PERCENTRANK(array, x)
//If X matches one of the values in the array, this function is
//equivalent to the Excel formula =(RANK(x)-1)/(N-1) where N is the number of data points.
//If X does not match one of the values, then the PERCENTRANK function interpolates.
// http://www.pcreview.co.uk/forums/algorithm-computing-excel-percentrank-t946312.html
array.Sort();
double result = 0;
bool foundX = false;
for (int index = 0; index < array.Count; index++)
{
if (array[index] == x)
{
result = ((double)index) / ((double)array.Count - 1);
foundX = true;
break;
}
}
// calculate value using linear interpolation
if (foundX == false)
{
double x1, x2, y1, y2;
x1 = x2 = x;
for (int i = 0; i < array.Count - 1; i++)
{
if (array[i] < x && x < array[i + 1])
{
x1 = array[i];
x2 = array[i + 1];
foundX = true;
break;
}
}
if (foundX == true)
{
y1 = percentRank(array, x1);
y2 = percentRank(array, x2);
result = (((x2 - x) * y1 + (x - x1) * y2)) / (x2 - x1);
}
else
{
// use the smallest or largest value in the set which ever is closer to valueX
if (array[0] > x)
{
result = 0;
}
else
{
result = 1;
}
}
}
return result;
}
private静态双百分比秩(列表数组,双x)
{
//计算百分比秩(数组,x)
//如果X与数组中的一个值匹配,则此函数为
//等效于Excel公式=(秩(x)-1)/(N-1),其中N是数据点的数量。
//如果X与其中一个值不匹配,则PERCENTRANK函数将插值。
// http://www.pcreview.co.uk/forums/algorithm-computing-excel-percentrank-t946312.html
array.Sort();
双结果=0;
bool-foundX=false;
for(int index=0;indexx)
{
结果=0;
}
其他的
{
结果=1;
}
}
}
返回结果;
}
编辑:确定答案是隐式类型转换。我可以禁用它吗?我不喜欢它,因为它可能会产生一些我不知道的错误
我的问题是为什么
private static double percentRank(List<int> array, double x)
{
// Calculate the PERCENTRANK(array, x)
//If X matches one of the values in the array, this function is
//equivalent to the Excel formula =(RANK(x)-1)/(N-1) where N is the number of data points.
//If X does not match one of the values, then the PERCENTRANK function interpolates.
// http://www.pcreview.co.uk/forums/algorithm-computing-excel-percentrank-t946312.html
array.Sort();
double result = 0;
bool foundX = false;
for (int index = 0; index < array.Count; index++)
{
if (array[index] == x)
{
result = ((double)index) / ((double)array.Count - 1);
foundX = true;
break;
}
}
// calculate value using linear interpolation
if (foundX == false)
{
double x1, x2, y1, y2;
x1 = x2 = x;
for (int i = 0; i < array.Count - 1; i++)
{
if (array[i] < x && x < array[i + 1])
{
x1 = array[i];
x2 = array[i + 1];
foundX = true;
break;
}
}
if (foundX == true)
{
y1 = percentRank(array, x1);
y2 = percentRank(array, x2);
result = (((x2 - x) * y1 + (x - x1) * y2)) / (x2 - x1);
}
else
{
// use the smallest or largest value in the set which ever is closer to valueX
if (array[0] > x)
{
result = 0;
}
else
{
result = 1;
}
}
}
return result;
}
可以将整数指定给双精度值。C#将从Int32
隐式转换为Double
你可以在这里看到:
double value = 3;
这是允许的,因为相同的隐式转换。如果没有该转换,您必须键入:
double value = 3.0;
这在C语言规范第6.1.2节“隐式数字转换”中有规定
隐式数字转换为:
- 从int到long、float、double或decimal
C#编译器正在执行隐式强制转换操作。double可以保存任何整数值。存在从
int
到double
的隐式转换
转换是隐式的,因为double可以保存int的值而不会丢失精度
有从double到int的显式
转换,但没有隐式
转换。原因是,如果在int中存储一个double,当它截断小数点时,就会丢失值
MSDN对转换有很好的描述:一个int
可以隐式转换为双精度
。这就是这里发生的事情。我真的不明白这个问题。虽然您的变量称为“array”,但它实际上是一个列表,因此该函数被称为begin,并具有适当的类型。这似乎很好,但我强烈建议您将参数名称更改为更好的名称(例如cellValueList,以便您知道数据来自何处)。@BenjaminDangerJohnson他在问为什么他可以将Int32
传递到定义为接受双精度(“x”,而不是“数组”)的参数中你不打算重构这个方法吗?啊,很好。我以为他把他的列表混淆为int[]类型谢谢,愚蠢的问题-->我可以禁用这个隐式强制转换吗?因为它可能会在我的程序中引入我不知道的错误?@ClaytonLeung不。它是语言本身的一部分<如果将代码>Int32
传递给Double,则它始终隐式转换为Double
。