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

C#

 
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 
GeneralRe: foreach Dictionary Pin
The Nightcoder28-May-08 21:48
The Nightcoder28-May-08 21:48 
George_George wrote:
...delete during foreach is not hard to implement and should not be expensive


You are perfectly right - there are scenarios where this isn't expensive - a linked list is a perfect example. A lazy-loading collection class that behaves like an ADO recordset would be another.

The point, however, is that this isn't always the case - it depends on how the collection class is implemented internally.

Look at this example (not tested, may have typos):

private List<SomeType> myList = new List<SomeType>

// ...

// this is what supports foreach - it gets compiled into
// an IEnumerator class with MoveNext and Current methods
// (and i as a private field) by the compiler:
public System.Collections.Generic.IEnumerator<SomeType> GetEnumerator()
{
    for (int i = 0; i < myList.Length; i++)
    {
        yield return myList[i];
    }
}

// explicit implementation of non-generic version:
public System.Collections.Generic.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
    return this.GetEnumerator();
}


This is probably what most custom collection classes look like (at least mine do). Since I know that people don't mess with my collection from within a foreach loop, I don't even have to think about what happens if elements get deleted. As you can see, I will miss the next element in that case - if you delete element 2 in {1, 2, 3, 4}, the next element yielded will be 4, not 3. Also, if someone tried to optimize it by storing myList.Length in a variable (which would be quite possible), the for loop would even throw an exception (index out of range) before finishing the loop.

If you look at it for a while, you see that there is no really simple way to fix this (allow deletes) without using something that isn't a List. And let's say - for the sake of argument - that I have to use a list (perhaps because I get the list from some internal class that only returns a list, and a conversion would be costly).

So... although you could see the "rules" as more of recommendations, they are generally accepted, so you can never assume that they don't apply. Also, you will cause confusion among your peers if you violate them, even if the collection class you're using explicitly permits it (unless you make that very clear in a comment).

Peter the small turnip

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

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

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.