overloading
overloading

Reputation: 1200

foreach statement count

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

Answers (4)

Reza Owliaei
Reza Owliaei

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

David Osborne
David Osborne

Reputation: 6821

You can use a multiline lambda that includes a count increment.

Upvotes: 0

illegal-immigrant
illegal-immigrant

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

gdoron
gdoron

Reputation: 150313

Simply Count():

test.testMethod.Count()

Upvotes: 1

Related Questions