I have seen in different places that we should not use foreach to iterate through SPListItemCollection, but I can't find explanation, why?

E.g. http://de.slideshare.net/ghurlman/sharepoint-development-101/21

Bad:

foreach (SPListItem item in myList.Items)
{
  // loop
}

Good:

SPListItemsCollection items = myList.Items;
for (int i = 0; i < items.Count; i++)
{
  // loop
}

But the code is really different, we can get items and use it with foreach as well

SPListItemsCollection items = myList.Items;
foreach (SPListItem item in items)
{
  // loop
}

Which one should I use and why?

up vote 10 down vote accepted

Generally speaking you will want to use SPQuery to only query items you're interested in.

Unless you're doing

SPList oList = web.GetList("XYZ");
for(int i=0; i < oList.Items.Count; i++)
{
    string strLstItemName = oList.Items[i].Name; // << BAD, as you use Items here, so you fetch them from DB each loop
}

there shouldn't be any relevant difference between your bad and good examples. It's not like foreach would fetch the whole Items collection from DB on each loop.

See Kobi's answer here. Also, I don't think generic .NET performance aspects play much of a role here as DB roundtrip is any way the slowest. Of course, with HUGE list you may end up waiting 60 secs for the data to arrive from DB to SharePoint, and THEN you can save 1 sec in using for loop instead of foreach.

In the context of general c# comparistions between for and foreach - There is a pretty comphrensive discussion here

  1. for loops on List are a bit more than 2 times cheaper than foreach loops on List.
  2. Looping on array is around 2 times cheaper than looping on List.
  3. As a consequence, looping on array using for is 5 times cheaper than looping on List using foreach (which I believe, is what we all do).

The source quoted in the above discussion - here

  • Thank you for general comparistions between for and foreach. But, I have read the main source and decided to make tests by myself, and, I have found no significant difference between for and foreach. – Dmitry T Sep 15 '15 at 12:50

As long as you stick to the principle of storing the SPListItemCollection in a variable before looping through the items, the difference between using for instead of foreach is close to redundant.

The foreach loop will be converted into a for loop under the hood. It's a 1, perhaps 2 sec, difference.

So if you write:

var listItems = SPContext.Current.List.Items;
foreach (SPListItem item in listItems)
{
    Console.Write(item.Title);
}

Instead of:

var listItems = SPContext.Current.List.Items;
for (var i = 0; i < listItems.Count; i++)
{
    var item = listItems[i];
    Console.Write(item.Title);
}

Doesn't really make a difference.

Performance aside, there's a very compelling reason to use the for loop instead of foreach: Deletions. If you use a foreach--or even a for loop going forward (i.e. for (int i = 0; i < items.Count; i++))--you're going to end up, at best, skipping some items because the deletion will modify the collection as you're iterating through it. Thus, using a decrementing for loop is much better whenever modifying the collection's objects. Example:

SPListItemsCollection items = myList.Items;
for (int i = items.Count - 1; i >= 0; i--)
{
  SPListItem item = items[i];
  item.Delete();
}

This way, you don't end up pulling the rug out from under your feet.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.