Solved

.NET: Safety with foreach iterations

Posted on 2008-10-20
4
365 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

785 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