?
Solved

.NET: Safety with foreach iterations

Posted on 2008-10-20
4
Medium Priority
?
370 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

770 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