[英]Check empty element array of double c#
I'm looking for a code to check if an element of my array of double is empty.我正在寻找一个代码来检查我的 double 数组的元素是否为空。 I tried with
isNaN
, string.isNullOrEmpty
and 0.0D
but nothing to do, I still have this text : Not a Number.我尝试使用
isNaN
、 string.isNullOrEmpty
和0.0D
但无事可做,我仍然有这样的文字:不是数字。
So, do you know any code in C# which is able to check if an element in a an array of double is empty?那么,您知道 C# 中的任何代码可以检查 double 数组中的元素是否为空吗?
Here is my code:这是我的代码:
if (!Double.IsNaN(d1.getChiffreAffaireBase()[keyIndex1]))
{
textBox43.Text = calcMargeCa(d1.getChiffreAffaireBase()[keyIndex1], d1.getChiffreAffairePlus()[keyIndex1]).ToString("0.00");
textBox44.Text = calcMargeCa(d1.getChiffreAffaireBase()[keyIndex1+1], d1.getChiffreAffairePlus()[keyIndex1+1]).ToString("0.00");
}
else
{
label13.Hide();
textBox43.Hide();
textBox44.Hide();
}
if you declare an array like this如果你声明一个这样的数组
double[] array = new double[12]; // elements cannot be null
all elements will be set to 0
.所有元素都将设置为
0
。
Declare it as nullable
, if you really want将其声明为
nullable
,如果你真的想要
var array = new double?[12]; // elements can be null
var isEmpty = (array[6] == null);
or或者
var isEmpty = !array[6].HasValue;
Value types cannot not have a value (that is to say, they cannot be null
) but you could use a Nullable<double>
(or double?
for short) which can be set to null
if you want to indicate that it doesn't have a value.值类型不能没有值(也就是说,它们不能为
null
)但您可以使用Nullable<double>
(或double?
简称),如果您想表明它没有,可以将其设置为null
有一个价值。 Here's an example:下面是一个例子:
double?[] array = new double?[12];
then for checking if it has a value you compare it to null
:然后为了检查它是否有一个值,你将它与
null
进行比较:
if (array[6] == null){
// do your thing
}
Edit:编辑:
Off topic but now that you've posted your code I see that you're using double
to represent money (Chiffre d'Affaire or Revenue) while that could work, you should use decimal
instead题外话,但既然你已经发布了你的代码,我看到你使用
double
来表示金钱(Chiffre d'Affaire 或 Revenue)虽然这可以工作,但你应该使用decimal
代替
Double.IsNaN doesn't check for null values , check this for more or read the documentations about it https://stackoverflow.com/a/7967190/6001737 Double.IsNaN 不检查空值,请检查更多内容或阅读有关它的文档https://stackoverflow.com/a/7967190/6001737
instead you can use double?[] array = new double?[12];
相反,您可以使用
double?[] array = new double?[12];
which is Nullable and you can then check if a certain array value equals null这是 Nullable,然后您可以检查某个数组值是否等于 null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.