[英]What is the equivalent of Java's .length for arrays in C#?
I'm new to C#, and I'm trying to convert this code from java into C#. 我是C#的新手,我正在尝试将此代码从Java转换为C#。
static public double euclidean_2(double[] x, double[] y)
{
if (x.length != y.length) throw new RuntimeException("Arguments must have same number of dimensions.");
double cumssq = 0.0;
for (int i = 0; i < x.length; i++)
cumssq += (x[i] - y[i]) * (x[i] - y[i]);
return cumssq;
}
I know java uses .length but what is the equivalent in C# since I keep getting an error 我知道java使用.length但由于我不断收到错误,C#中的等效项是什么
Thanks 谢谢
In C# public members should be capitalized: 在C#中,公共成员应大写:
for (int i = 0; i < x.Length; i++)
cumssq += (x[i] - y[i]) * (x[i] - y[i]);
For the arrays, you want the Length
property. 对于数组,您需要
Length
属性。
Also the exception type should be changed. 另外,异常类型也应更改。
I think that covers it. 我认为涵盖了它。
the length
should be Length
, since members that are public should be capitalized so your code in C# should be like this: 该
length
应为Length
,因为公共成员应大写,因此您在C#中的代码应如下所示:
public static double euclidean_2(double[] x, double[] y){
if (x.Length != y.Length){
throw new Exception("Arguments must have same number of dimensions.");
}
double cumssq = 0.0;
for (int i = 0; i < x.Length; i++){
cumssq += (x[i] - y[i]) * (x[i] - y[i]);
}
return cumssq;
}
also take note of the keyword Exception
instead of Runtime Exception
还请注意关键字
Exception
而不是Runtime Exception
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.