Solved

.NET: Safety with foreach iterations

Posted on 2008-10-20
4
366 Views
Last Modified: 2013-12-17
I heard that when iterating through a collection using foreach statement it is not wise to change the content of the collection. Does this mean that one cannot change properties of the collection entries. I mean that if, for instance, I have Employee objects in a collection, can I change some attribute of each Employee while looping. I know it is possible syntaxically and the compile won't say anything. Is it a good practice.
0
Comment
Question by:karakav
4 Comments
 
LVL 18

Expert Comment

by:philipjonathan
ID: 22755700
Changing property of elements is ok. What you cannot do is adding or removing collection elements
0
 
LVL 13

Accepted Solution

by:
TechTiger007 earned 500 total points
ID: 22755747
You can modify the properties, but you should not be deleting the objects. Deleting an object will modify the indexes of the objects in the collection and hence you wont get the required result. This is not just with foreach loop it is for all loops.

for(int i=0; i<collection.length;i++)
{
  if(i%2)=0
  //delete the object
}
in this scenario i=0 will gets deleted now object at index 1 will become index 0 during the next iteration when it checks for i=1 you will be actually checking on the object which was originally on i=2 but now moved to 1 since you have deleted the object  at 0.

modifying the properties is fine

I hope  this helps
0
 
LVL 6

Expert Comment

by:openshac
ID: 22755794
if you want to delete an object run the loop backwards.

The following will work fine:
for(int i=collection.Length-1; i >=0; i--)
{
 if(i%2)=0
 //delete the object
}

Open in new window

0
 
LVL 4

Author Closing Comment

by:karakav
ID: 31507740
Thanks.
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question