Change A Property Within The Foreach Loop
-
Is it ok in .Net 3.5 to change a property within a foreach loop? Does it still run the risk of producing unpredictable results? Also, will the potential results be noticeable or might it cause some type of slow, inconspicuous but nonetheless catastrophic error. For example: foreach(Employee janitor in EmployeesWithPayIncrease) { janitor.Salary += 5000; } Thanks for any help or information you can provide.
-
Is it ok in .Net 3.5 to change a property within a foreach loop? Does it still run the risk of producing unpredictable results? Also, will the potential results be noticeable or might it cause some type of slow, inconspicuous but nonetheless catastrophic error. For example: foreach(Employee janitor in EmployeesWithPayIncrease) { janitor.Salary += 5000; } Thanks for any help or information you can provide.
It is fine to change a property; the only thing you can't do is change the loop variable or the list itself. So
foreach(Employee janitor in EmployeesWithPayIncrease)
{
janitor.Salary += 5000;
}is fine, but
foreach(Employee janitor in EmployeesWithPayIncrease)
{
janitor = new Employee();
}or
foreach(Employee janitor in EmployeesWithPayIncrease)
{
EmployeesWithPayIncrease.Remove(janitor);
}are not, as it would mean it would compromise the enumeration.
If Barbie is so popular, why do you have to buy her friends? Eagles may soar, but weasels don't get sucked into jet engines. If at first you don't succeed, destroy all evidence that you tried.
-
It is fine to change a property; the only thing you can't do is change the loop variable or the list itself. So
foreach(Employee janitor in EmployeesWithPayIncrease)
{
janitor.Salary += 5000;
}is fine, but
foreach(Employee janitor in EmployeesWithPayIncrease)
{
janitor = new Employee();
}or
foreach(Employee janitor in EmployeesWithPayIncrease)
{
EmployeesWithPayIncrease.Remove(janitor);
}are not, as it would mean it would compromise the enumeration.
If Barbie is so popular, why do you have to buy her friends? Eagles may soar, but weasels don't get sucked into jet engines. If at first you don't succeed, destroy all evidence that you tried.
-
So can I! But I figure that if he is asking a basic (sensible, but basic) question like that, I didn't want to confuse him with custom comparisons, etc. I found it refreshing to be politely asked a sensible question :laugh:
If Barbie is so popular, why do you have to buy her friends? Eagles may soar, but weasels don't get sucked into jet engines. If at first you don't succeed, destroy all evidence that you tried.
-
So can I! But I figure that if he is asking a basic (sensible, but basic) question like that, I didn't want to confuse him with custom comparisons, etc. I found it refreshing to be politely asked a sensible question :laugh:
If Barbie is so popular, why do you have to buy her friends? Eagles may soar, but weasels don't get sucked into jet engines. If at first you don't succeed, destroy all evidence that you tried.
-
Is it ok in .Net 3.5 to change a property within a foreach loop? Does it still run the risk of producing unpredictable results? Also, will the potential results be noticeable or might it cause some type of slow, inconspicuous but nonetheless catastrophic error. For example: foreach(Employee janitor in EmployeesWithPayIncrease) { janitor.Salary += 5000; } Thanks for any help or information you can provide.
Thanks for the information. I appreciate the answer. However, I am curious: how could you make the first piece of code fail? Also, my code will work without unintended consequences, right?
-
Thanks for the information. I appreciate the answer. However, I am curious: how could you make the first piece of code fail? Also, my code will work without unintended consequences, right?
BlitzPackage wrote:
how could you make the first piece of code fail
The setter of that property could be changing the collection in some way
BlitzPackage wrote:
Also, my code will work without unintended consequences, right?
Well that depends on what the property is doing..
-
Is it ok in .Net 3.5 to change a property within a foreach loop? Does it still run the risk of producing unpredictable results? Also, will the potential results be noticeable or might it cause some type of slow, inconspicuous but nonetheless catastrophic error. For example: foreach(Employee janitor in EmployeesWithPayIncrease) { janitor.Salary += 5000; } Thanks for any help or information you can provide.
You can't modify an object in a
foreach
loop like that. You have to loop differently (for
,while
, etc).45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
You can't modify an object in a
foreach
loop like that. You have to loop differently (for
,while
, etc).45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001Yes you can - provided the property you modify does not alter the comparison, or add / remove items from the collection. Increasing the salary would not be a problem. :-D
If Barbie is so popular, why do you have to buy her friends? Eagles may soar, but weasels don't get sucked into jet engines. If at first you don't succeed, destroy all evidence that you tried.
-
Is it ok in .Net 3.5 to change a property within a foreach loop? Does it still run the risk of producing unpredictable results? Also, will the potential results be noticeable or might it cause some type of slow, inconspicuous but nonetheless catastrophic error. For example: foreach(Employee janitor in EmployeesWithPayIncrease) { janitor.Salary += 5000; } Thanks for any help or information you can provide.
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.