Click here to Skip to main content
16,003,873 members
Home / Discussions / C#
   

C#

 
GeneralRe: Change A Property Within The Foreach Loop Pin
harold aptroot6-Feb-10 4:22
harold aptroot6-Feb-10 4:22 
GeneralRe: Change A Property Within The Foreach Loop Pin
OriginalGriff6-Feb-10 4:29
mveOriginalGriff6-Feb-10 4:29 
GeneralRe: Change A Property Within The Foreach Loop Pin
harold aptroot6-Feb-10 4:37
harold aptroot6-Feb-10 4:37 
AnswerRe: Change A Property Within The Foreach Loop Pin
BlitzPackage6-Feb-10 4:45
BlitzPackage6-Feb-10 4:45 
GeneralRe: Change A Property Within The Foreach Loop Pin
harold aptroot6-Feb-10 5:12
harold aptroot6-Feb-10 5:12 
AnswerRe: Change A Property Within The Foreach Loop Pin
#realJSOP6-Feb-10 5:44
professional#realJSOP6-Feb-10 5:44 
GeneralRe: Change A Property Within The Foreach Loop Pin
OriginalGriff6-Feb-10 9:00
mveOriginalGriff6-Feb-10 9:00 
AnswerRe: Change A Property Within The Foreach Loop Pin
AspDotNetDev6-Feb-10 19:18
protectorAspDotNetDev6-Feb-10 19:18 
When a foreach is applied to a collection, C# will have the collection return an enumerator (I believe by calling a method, GetEnumerator). With the built in .Net types, those enumerators have logic in them that throw an exception if they detect that the list has been changed since the enumerator was created. They do this by taking a snaphot of the version number stored in the collection. Whenever a collection is modified, .Net increments this version number. And whenever an element is gotten from the enumerator, it checks it's version number against the collection it was created from.

The version number is only incremented for operations that change the structure of the list (reordering items, item removal, item additions, assigning items, and so on), not ones that change simple attributes of the list.

Also, most programmers are not aware that this version incrementing and checking must be done, so many custom collections will not throw an exception when the collection was modified in a foreach. For example, you could yourself create a custom collection that doesn't check if the collection was modified. Then no exception would be thrown when you modify the collection (add items, delete them, etc).

Whether or not modifying the collection during a foreach would cause an error (note that I didn't say "exception", I said "error") entirely depends on the collection.
Questionforms & files [modified] Pin
jojoba20106-Feb-10 2:31
jojoba20106-Feb-10 2:31 
QuestionMulti dimension Array Not Working [Solved] Pin
Wamuti6-Feb-10 2:07
Wamuti6-Feb-10 2:07 
AnswerRe: Multi dimension Array Not Working Pin
harold aptroot6-Feb-10 2:18
harold aptroot6-Feb-10 2:18 
QuestionProblem with Interface? Pin
mdrizwan_15-Feb-10 23:13
mdrizwan_15-Feb-10 23:13 
AnswerRe: Problem with Interface? Pin
Eddy Vluggen5-Feb-10 23:24
professionalEddy Vluggen5-Feb-10 23:24 
GeneralRe: Problem with Interface? Pin
OriginalGriff5-Feb-10 23:29
mveOriginalGriff5-Feb-10 23:29 
GeneralRe: Problem with Interface? Pin
Eddy Vluggen5-Feb-10 23:38
professionalEddy Vluggen5-Feb-10 23:38 
AnswerRe: Problem with Interface? Pin
OriginalGriff5-Feb-10 23:28
mveOriginalGriff5-Feb-10 23:28 
AnswerRe: Problem with Interface? Pin
Md. Marufuzzaman6-Feb-10 0:14
professionalMd. Marufuzzaman6-Feb-10 0:14 
AnswerRe: Problem with Interface? Pin
Luc Pattyn6-Feb-10 1:49
sitebuilderLuc Pattyn6-Feb-10 1:49 
AnswerRe: Problem with Interface? Pin
#realJSOP6-Feb-10 2:03
professional#realJSOP6-Feb-10 2:03 
Questionc# Pin
bahar35-Feb-10 23:07
bahar35-Feb-10 23:07 
QuestionRe: c# Pin
Eddy Vluggen5-Feb-10 23:17
professionalEddy Vluggen5-Feb-10 23:17 
AnswerRe: c# Pin
OriginalGriff5-Feb-10 23:32
mveOriginalGriff5-Feb-10 23:32 
AnswerRe: c# Pin
mdrizwan_15-Feb-10 23:33
mdrizwan_15-Feb-10 23:33 
AnswerRe: c# Pin
Mycroft Holmes6-Feb-10 0:06
professionalMycroft Holmes6-Feb-10 0:06 
AnswerRe: c# Pin
Md. Marufuzzaman6-Feb-10 0:33
professionalMd. Marufuzzaman6-Feb-10 0:33 

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.