Solved

.NET: Safety with foreach iterations

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

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
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…

691 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