问题描述
我已经声明了一个int数组,并将值传递给它.但计数保持不变.当我将2个值添加到它.它显示了已声明数组的总数.
我的代码是这样的:
Hi,
I have declare a int array, and passing the values to it. but the count remains same. As i am adding 2 values to it. it shows me the total count of declared array.
My code goes like this:
public int[] Stub{ get; set; }
在方法中:
In a Method:
SerPro obj = new SerPro ();
obj.Stub = new int[5];
由于我处于某种状况,如果通过了,我将为其输入值.
As i am having some condition, if it passes i will enter the value to it.
obj.Stub [i] = sr.Stub ;
然后我想要插入值的计数.我把这个放了:
Then i want the count of the inserted values. And i put this one:
int c = obj.Stub .Count<int>(value => value != null);
这段代码将获得数组的计数为5,但是我将值插入了两次.它获取带有和不带有值的总数组的计数.但我只想要插入值的计数.
问候,
Basha.s
This code will get the count of the array as 5, but i inserted the values for two times. it get the count of the total array with and without values. but i want the count of inserted values only.
Regards,
Basha.s
推荐答案
obj.Stub.Count(p => p != 0)
值类型不可为空,因此默认状态为0.我建议使用List< int>以及.
Value types are not nullable so 0 is the default state. I would suggest going with a List<int> as well.
这篇关于我如何计算int数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!