Click here to Skip to main content
15,915,093 members
Home / Discussions / C#
   

C#

 
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 
GeneralRe: foreach Dictionary Pin
George_George28-May-08 15:49
George_George28-May-08 15:49 
Thanks PeterTheSwede!


Great reply! I am interested to discuss further with you.

I am very interested in two statements you mentioned before,

---------------------
1.

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.

2.

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.
---------------------

Especially for item 2, I think it is the reason why add/remove/reorder during foreach is expensive, but my question is I can not figure out why from the implmentation level it is expensive, for example, why do we need to "copy a reference for each element into a temporary array when it is created"? Could you clarify or give more description please?

The reason why I am confused is, for example, delete during foreach is not hard to implement and should not be expensive,

(suppose we are using linked list to store a list of key collections of a Dictinoary)

1. if delete an element which is already iterated, just remove and step next in next iteration;
2. if delete an element which is not iterated yet, just remove and step next in next iteration;
3. if delete an element which is the element which is currently iterated now, just remove the current and step to next iteration.


regards,
George
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 
GeneralRe: How to keep track of background threads? Pin
ptr2void27-May-08 3:15
ptr2void27-May-08 3:15 

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.