Using Generic Method to Determine if All Elements in an Array is Zero
-
I want to know how to do that. I can use foreach and for loop, but I prefer to use the array class. Assume I have my array like
int[] arrayData = { 1, 3, 5, 0, 6 };
By using .All generic method, it is possible to do something like that to determine if all elements are zero
arrayData.All //this is where I have the problem
So how can I use the .All to determine if all elements in the array is 0
-
I want to know how to do that. I can use foreach and for loop, but I prefer to use the array class. Assume I have my array like
int[] arrayData = { 1, 3, 5, 0, 6 };
By using .All generic method, it is possible to do something like that to determine if all elements are zero
arrayData.All //this is where I have the problem
So how can I use the .All to determine if all elements in the array is 0
The MSDN has samples, but basically you write a predicate method that returns what you want to return. For example, a search method would involve you writing the code that checks each element. Under the hood, it's not much different at the end of the day, the code that runs will use for each to check each element.
Christian Graus Driven to the arms of OSX by Vista.
-
I want to know how to do that. I can use foreach and for loop, but I prefer to use the array class. Assume I have my array like
int[] arrayData = { 1, 3, 5, 0, 6 };
By using .All generic method, it is possible to do something like that to determine if all elements are zero
arrayData.All //this is where I have the problem
So how can I use the .All to determine if all elements in the array is 0
arrayData.All(item => item == 0);
-
arrayData.All(item => item == 0);
-
item
is an inline-declared parameter. You shouldn't have to explicitly declare it anywhere.Enumerable.All
should be able to determine the type of the parameter at compile-time. Note: I'm not in front of my compiler right now, but it's possible (?) that you may have to explicitly provide the parameter with a type. In that case, I believe the syntax would be:arrayData.All((int item) => item == 0);
(disclaimer: this is all coming from memory, so I might not be spot-on :doh:)
-
item
is an inline-declared parameter. You shouldn't have to explicitly declare it anywhere.Enumerable.All
should be able to determine the type of the parameter at compile-time. Note: I'm not in front of my compiler right now, but it's possible (?) that you may have to explicitly provide the parameter with a type. In that case, I believe the syntax would be:arrayData.All((int item) => item == 0);
(disclaimer: this is all coming from memory, so I might not be spot-on :doh:)
-
item
is an inline-declared parameter. You shouldn't have to explicitly declare it anywhere.Enumerable.All
should be able to determine the type of the parameter at compile-time. Note: I'm not in front of my compiler right now, but it's possible (?) that you may have to explicitly provide the parameter with a type. In that case, I believe the syntax would be:arrayData.All((int item) => item == 0);
(disclaimer: this is all coming from memory, so I might not be spot-on :doh:)