Click here to Skip to main content
16,016,227 members
Home / Discussions / C#
   

C#

 
GeneralRe: foreach Dictionary Pin
George_George27-May-08 23:43
George_George27-May-08 23:43 
AnswerRe: foreach Dictionary Pin
The Nightcoder27-May-08 11:24
The Nightcoder27-May-08 11:24 
GeneralRe: foreach Dictionary Pin
George_George27-May-08 23:41
George_George27-May-08 23:41 
GeneralRe: foreach Dictionary Pin
Guffa28-May-08 2:11
Guffa28-May-08 2:11 
GeneralRe: foreach Dictionary Pin
George_George28-May-08 15:35
George_George28-May-08 15:35 
GeneralRe: foreach Dictionary Pin
Guffa28-May-08 22:32
Guffa28-May-08 22:32 
GeneralRe: foreach Dictionary Pin
George_George31-May-08 2:37
George_George31-May-08 2:37 
GeneralRe: foreach Dictionary Pin
The Nightcoder28-May-08 2:44
The Nightcoder28-May-08 2:44 
What Guffa said, plus some further clarifications (I hope):

The reason behind all this is that it is usually expensive (in terms of performance or memory) for an enumerator (see below) to handle additions/deletions/reordering during its lifetime.

An enumerator is the object (implementing the interfaces IEnumerator and possibly IEnumerator<T>) that handles the foreach loop. It is returned by the GetEnumerator method on the IEnumerable or IEnumerable<T> interface, which gets called behind the scenes by the foreach statement (in C# this is hidden - the GetEnumerator body can be thought of as the enumerator object).

As the enumerator has to know where it is and where it is going in the collection, handling inserts, deletes and reordering would force it to - for example - copy a reference for each element into a temporary array when it is created (when the loop starts), and then step through that array. Depending on the number of elements, this could take a lot of time (and space). And it's usually considered unwise to let something that looks simple (foreach) actually be complex behind the scenes (because that would fool programmers and make them write slow code by mistake).

Any clearer? See the docs (under System.Collections and System.Collections.Generic, respectively) for details on the interfaces I mention.

One more point: It is sometimes possible to figure out (or even obvious) exactly how a particular collection is implemented and exactly what happens in the enumerator. Knowing that, it's perfectly possible to perform deletions, additions and/or reorderings from within a foreach loop, as long as you take care and handle the consequences correctly (such as never seeing the item after an item getting deleted - which is true in index-based collections). But... it is normally not a good idea to do this - as the implementation may change in a newer version of the library defining the collection.

Also, I would consider it bad style to do inserts, deletions or reordering in a foreach loop even if a collection explicitly supports it (in its documentation, version-independently) - simply because it is confusing. Not everyone reading the foreach loop may have access to the docs, and would therefore assume the behaviour to be a bug.

Peter the small turnip

(1) It Has To Work. --RFC 1925[^]

GeneralRe: foreach Dictionary Pin
George_George28-May-08 15:49
George_George28-May-08 15:49 
GeneralRe: foreach Dictionary Pin
The Nightcoder28-May-08 21:48
The Nightcoder28-May-08 21:48 
GeneralRe: foreach Dictionary Pin
George_George31-May-08 2:36
George_George31-May-08 2:36 
GeneralRe: foreach Dictionary Pin
The Nightcoder2-Jun-08 2:20
The Nightcoder2-Jun-08 2:20 
GeneralRe: foreach Dictionary Pin
George_George3-Jun-08 2:36
George_George3-Jun-08 2:36 
GeneralRe: foreach Dictionary Pin
supercat96-Nov-08 9:12
supercat96-Nov-08 9:12 
GeneralRe: foreach Dictionary Pin
The Nightcoder28-May-08 22:01
The Nightcoder28-May-08 22:01 
Questionatomic operation of reference assignment? Pin
George_George27-May-08 3:40
George_George27-May-08 3:40 
AnswerRe: atomic operation of reference assignment? Pin
Guffa27-May-08 4:30
Guffa27-May-08 4:30 
GeneralRe: atomic operation of reference assignment? Pin
George_George27-May-08 23:54
George_George27-May-08 23:54 
GeneralRe: atomic operation of reference assignment? Pin
supercat910-Jul-08 7:07
supercat910-Jul-08 7:07 
AnswerRe: atomic operation of reference assignment? Pin
Guffa10-Jul-08 16:15
Guffa10-Jul-08 16:15 
GeneralRe: atomic operation of reference assignment? Pin
supercat910-Jul-08 18:21
supercat910-Jul-08 18:21 
AnswerRe: atomic operation of reference assignment? Pin
Zoltan Balazs27-May-08 4:46
Zoltan Balazs27-May-08 4:46 
GeneralRe: atomic operation of reference assignment? Pin
George_George27-May-08 23:55
George_George27-May-08 23:55 
QuestionHow to keep track of background threads? Pin
ptr2void27-May-08 3:09
ptr2void27-May-08 3:09 
AnswerRe: How to keep track of background threads? Pin
N a v a n e e t h27-May-08 3:12
N a v a n e e t h27-May-08 3:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.