Iterating a list of objects with foreach
Asked Answered
S

5

8

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"

Sabin answered 29/6, 2016 at 12:19 Comment(3)
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 #4005255Withdrawn
where did you find that quote?Myopic
I found that in a course about C# generic types in PS (PS is the abbreviation of a well known online courses provider)Sabin
I
12
foreach(var foo in foos)
{
  foo = null; // WRONG, foo is not editable
  foo.name = "John";  // RIGHT, foo properties are editable
}
Immolate answered 29/6, 2016 at 12:21 Comment(11)
Doesnt foo.name = "John" modify the object instance?? (so the instance IS editable)Sabin
@cnom, it looks like you simply don't understand what is instance.Extraterritoriality
"...object instance is not editable, but the object properties are editable..." = "...foo is not editable, but foo.name are editable..."Immolate
Sorry for the question, but some comments in other answers mixed things up... Is foo, or foos the iterated object?Sabin
foos is a collection of fooImmolate
You cannot either modify the collection foos without throwing an InvalidOperationExceptionIlianailine
@Stanley , my question is "What is the iterated object", not what is foosSabin
@Sabin foos is the iterated object. More specifically, the iterated collectionIlianailine
Example: foo[] foos = new foo[] {foo1, foo2, foo3, ...};Immolate
@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!Sabin
@Sabin - 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.Pullover
K
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";
}
Konrad answered 29/6, 2016 at 12:22 Comment(3)
this second snippet refers to the list, not the iterated object!Sabin
@Sabin the second snippet refers to the object, not the list !Konrad
Sorry i meant the opposite, the first snippet refers to the list... :)Sabin
F
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

Frontward answered 29/6, 2016 at 12:21 Comment(0)
C
0
foreach var car in cars 
{
    //you can edit car.color here
    //you cannot edit car
}
Candelaria answered 29/6, 2016 at 12:21 Comment(0)
H
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.

Husbandry answered 29/6, 2016 at 12:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.