Reputation: 1200
How do I get the count/number of execution of a foreach
statement?
For example if I have a statement like this:
test.testMethod.Foreach(x => x.testMethod2.Add(test_arg));
I want to know the number of times Add
has run, basically the number of x
.
What's the simplest way to do this?
Upvotes: 0
Views: 171
Reputation: 3373
I think you need your item index in each iteration:
var index = 0;
test.testMethod.Foreach(x =>{
++index;
x.testMethod2.Add(test_arg);
});
Upvotes: 1
Reputation: 6821
You can use a multiline lambda that includes a count increment.
Upvotes: 0
Reputation: 8254
There is no Foreach
in LINQ
. You are talking about List<T> ForEach
.
Use Count
on List<T>
to get number of items
Upvotes: 4