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

C#

 
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 
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 
George_George wrote:
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?


Expanding on my previous example:

// my private list:
private List<SomeType> myList = new List<SomeType>
 
// other code...
 
// super-robust enumerator:
public System.Collections.Generic.IEnumerator<SomeType> GetEnumerator()
{
    SomeType[] elems = myList.ToArray(); // EXPENSIVE!
 
    for (int i = 0; i < elems.Length; i++)
    {
        yield return elems[i];
    }
}
 
// explicit implementation of non-generic version:
public System.Collections.Generic.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
    return this.GetEnumerator();
}


With the ToArray() call (which is a memory allocation and a shallow copy of the list - an expensive operation if the list is large), I am now totally immune to any changes of the list during the foreach.

And... without spending more time on it than I would like when building my own collection class, I can't think of a cheaper way to solve this (again assuming that I must use a list internally, and cannot use a linked list, for example). And again... the purpose of the rule (to not mess with the contents of a collection from within a foreach loop) is that I shouldn't have to spend time thinking about this when building an enumerator...

Any clearer?

Peter the small turnip

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

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 
GeneralRe: How to keep track of background threads? Pin
N a v a n e e t h27-May-08 3:18
N a v a n e e t h27-May-08 3:18 
GeneralRe: How to keep track of background threads? Pin
ptr2void27-May-08 3:24
ptr2void27-May-08 3:24 
GeneralRe: How to keep track of background threads? Pin
N a v a n e e t h27-May-08 3:41
N a v a n e e t h27-May-08 3:41 
GeneralRe: How to keep track of background threads? Pin
ptr2void27-May-08 3:52
ptr2void27-May-08 3:52 
GeneralRe: How to keep track of background threads? Pin
supercat912-Jul-08 12:21
supercat912-Jul-08 12:21 
QuestionWord Plug-in eventhandlers stops working... Pin
Anders Molin27-May-08 3:00
professionalAnders Molin27-May-08 3:00 

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.