Solved

.NET: Safety with foreach iterations

Posted on 2008-10-20
4
359 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now