views:

164

answers:

5

Hi,
Some time ago I read that foreach works with "copies" of objects and thus it can be used for information retrieval instead of its updating. I do not get it as it is entirely possible to loop through list of classes and change its field. Thanks!

+9  A: 

What you may have read is that you can't modify a collection while iterating over it using foreach whereas you can (if you're careful) using a for loop. For example:

using System;
using System.Collections.Generic;

class Test
{
    static void Main()
    {
        var list = new List<int> { 1, 4, 5, 6, 9, 10 };


        /* This version fails with an InvalidOperationException
        foreach (int x in list)
        {
            if (x < 5)
            {
                list.Add(100);
            }
            Console.WriteLine(x);
        }
         */

        // This version is okay
        for (int i = 0; i < list.Count; i++)
        {
            int x = list[i];
            if (x < 5)
            {
                list.Add(100);
            }
            Console.WriteLine(x);            
        }
    }
}

If that's not what you were referring to, please give more details - it's hard to explain what you've read without knowing exactly what it said.

Jon Skeet
Jon, thanks. Yes I read it and understand that I cannot delete an element of e.g.array if looping through it. But I think I can modify each elements content, right?
Radusko
Another caveat is that foreach may be implemented very differently than the indexer used by for(). In the case of the listview's SelectedItems collection, it makes a HUGE difference in terms of performance.
EricLaw -MSFT-
@Radusko: Yes, you can modify the data in the object being referred to... although if it's a value type, you'll still be dealing with a copy. (Hopefully you won't have to deal with mutable value types though.)
Jon Skeet
@Jon Skeet: Thanks. No these are not value types, I referred to classes. I just wanted to be sure I had understood.
Radusko
@Jon Skeet: However, if I loop through an array of Strings, I will not be able to change it. String is immutable but reference, that is the reason?
Radusko
@Radusko: Well, that depends on how you're trying to change it. You can't change the *content* of a string (other than with reflection). Looping over a string array with a for loop will let you change the contents of the *array* though.
Jon Skeet
@Jon Skeet:I cannot change the content..but if I call String.Remove method, it is allowed. I do get it now :(
Radusko
@Radusko: But `string.Remove` just returns a *new* string with some text removed. It doesn't change the existing string.
Jon Skeet
+1  A: 

You cannot modify the element in a foreach:

var list = new List<string>();
list.AddRange(new string[] { "A", "B", "C" });

foreach (var i in list)
{
    // compilation error: Cannot assign 'i' because it is a 'foreach iteration variable'
    i = "X";
}

Although when working with for you are accessing the element on the list with its index, and not the iterator, so this way you can modify the collection.

BrunoLM
Actually you can modify the elements (if they can be modified) that are reference types. You can't replace, add, or delete them. The reason your code won't work is because a string is immutable. Once created a string can't be modified in any way. What appears to be modification is actually a replacement of one string with another. Thus in the illustration you gave above you replace one of the elements (or delete one and add one) and is not allowed when enumerating a collection.
Kirk
+1  A: 

foreach use an Iterator to get each element of a sequence. The sequence can be Anything that implements IEnumerable. The IEnumerable does not need to be finite (the sequence 0 1 2 3 4... 1000... ) is IEnumerable.

for is only a C# constructs which allow you to declare a loop (used to do all sort of things, not only iterating through collections)

It's worth noting that the Iterator implementation in .NET for the collections does not support sequence modification during iteration.

Maupertuis
+1  A: 

foreach is using IEnumerable to loop through collection. This makes it impossible to modify this collection(remove, add items), but you still can modify objects inside, if they are reference types.

for is simple combination of simple loop combined with direct access to items in collection. There is no kind of blocking while this loop is going.

Euphoric
A: 

Compare it with a readonly field:

private readonly List<int> MyList = new List<int>();

Now, in this code I cannot do MyList = new List<int>() as that will alter what MyList points to, but I can alter the list pointed to with MyList.Add(3).

Likewise, you cannot alter the variable used by the foreach iteration, but can what it refers to:

foreach(List<int> lst in MyListOfLists)
{
  lst = new List<int>(); // not allowed
  lst.Add(3);  // allowed
}

Finally, the enumerator used to implement foreach is not required to remain valid if the underlying collection is being used:

foreach(int x in SomeEnumerable)
{
    if(x != 0)
        SomeEnumerable.Add(0);
}

Assuming that Add modifies SomeEnumerable then the above might work, it might "work" in a strange and hard to understand way and it might throw an exception. No behaviour is guaranteed with such code and modifying a collection during enumeration is considered incorrect for this reason.

Jon Hanna