7

I came across this statement:

"When using foreach on a list of objects, the iterated object instance is not editable, but the object properties are editable"

Could someone demonstrate the above with a simple example, please?

Let me re-phrase (as I found the statement in two versions), maybe this statement is more clear:

"When using foreach on a list of elements, the iteration variable that provides the element is readonly, but the element properties are editable"

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
cnom
  • 3,071
  • 4
  • 30
  • 60
  • It means the list itself cannot be modified (you cannot remove or add items while you are in the loop), but the properties of the items in the list can be modified. Also read http://stackoverflow.com/questions/4004755/why-is-foreach-loop-read-only-in-c-sharp – L-Four Jun 29 '16 at 12:21
  • where did you find that quote? – Thorarins Jun 29 '16 at 12:23
  • I found that in a course about C# generic types in PS (PS is the abbreviation of a well known online courses provider) – cnom Jun 29 '16 at 12:25

5 Answers5

12
foreach(var foo in foos)
{
  foo = null; // WRONG, foo is not editable
  foo.name = "John";  // RIGHT, foo properties are editable
}
  • Doesnt foo.name = "John" modify the object instance?? (so the instance IS editable) – cnom Jun 29 '16 at 12:27
  • 2
    @cnom, it looks like you simply don't understand what is [instance](http://stackoverflow.com/q/2219566/1997232). – Sinatr Jun 29 '16 at 12:28
  • "...object instance is not editable, but the object properties are editable..." = "...foo is not editable, but foo.name are editable..." –  Jun 29 '16 at 12:29
  • Sorry for the question, but some comments in other answers mixed things up... Is foo, or foos the iterated object? – cnom Jun 29 '16 at 12:40
  • foos is a collection of foo –  Jun 29 '16 at 12:40
  • You cannot either modify the collection *foos* without throwing an `InvalidOperationException` – Panda Jun 29 '16 at 12:41
  • @Stanley , my question is "What is the iterated object", not what is foos – cnom Jun 29 '16 at 12:42
  • @cnom *foos* is the iterated object. More specifically, the iterated collection – Panda Jun 29 '16 at 12:43
  • Example: foo[] foos = new foo[] {foo1, foo2, foo3, ...}; –  Jun 29 '16 at 12:44
  • @Stanley, so that example, does not demonstrate what I asked for. It is modifying foo, not the iterated object, in this case Lee's example demonstrates it more clearly! – cnom Jun 29 '16 at 12:44
  • 1
    @cnom - It's difficult to answer this question without a concrete example, but it is possible that the terminology in the original statement is wrong. This answer demonstrates that the iteration _variable_ cannot be modified, but the object instance it currently refers to can be mutated through it. – Lee Jun 29 '16 at 12:47
1

What is means is that the items in the list can't change whilst iterating, but the contents of the items may.

this will alter the collection and prevent the foreach completing:

foreach(var item in collection)
{
   collection.Remove(item);
}

This will change an item in the list and not prevent the foreach completing:

foreach(var item in collection)
{
    item.name = "Neil";
}
Neil
  • 11,059
  • 3
  • 31
  • 56
0

Yes

Foreach (n in list) if (n.something==true) list.Remove(n);

this will fail

you cannot remove an item in list, unlike say a for loop

BugFinder
  • 17,474
  • 4
  • 36
  • 51
0
foreach var car in cars 
{
    //you can edit car.color here
    //you cannot edit car
}
Andrew
  • 388
  • 2
  • 9
lolex
  • 65
  • 11
0

Not sure you need an example for this. You will step over each object in the collection, and you can do what you like to each of those objects, but you can't make changes to the collection itself, e.g. Insert, Remove, Clear, etc. Attempting such will throw an exception.

LordWilmore
  • 2,829
  • 2
  • 25
  • 30