简体   繁体   中英

C# Difference between Foreach and for (not performance)


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!

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.

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM