Reputation: 7778
I have this loop:
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
int listCount = list.Count;
for (int i = 0; i < listCount; i++) // Loop through List with for
{
if (i == 2)
{
list.Add(666);
listCount++;
}
System.Diagnostics.Debug.Write(list[i]);
}
I am wondering how do I apply the same logic to foreach loop? (add more elements inside the foreach loop).
Something like this:
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
foreach (int i in list)
{
if (i == 2)
{
list.Add(666);
//do something to refresh the foreach loop
}
System.Diagnostics.Debug.Write(list[i]);
}
Upvotes: 3
Views: 4511
Reputation: 186718
If you're looking for a consize solution you can use Linq instead of either for loop or foreach loop (you can't change the collection within foreach loop):
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
// add required "666" items
list.AddRange(Enumerable.Repeat(666, list.Count(x => x == 2)));
// print out all items
foreach(item in list)
System.Diagnostics.Debug.Write(item);
Upvotes: 1
Reputation: 1000
Not exactly what you're trying to do but you could do something like this...
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
List<int> list2 = new List<int>();
list2 = list;
foreach (int i in list)
{
if (i == 2)
{
list2.Add(666);
}
System.Diagnostics.Debug.Write(list[i]);
i++; // increment i
}
list = list2;
Upvotes: 1
Reputation: 223277
I am wondering how do I apply the same logic to foreach loop?
You shouldn't and you can't. Collection can't be modified inside a foreach
loop.
See: foreach, in (C# Reference)
If you need to add or remove items from the source collection, use a for loop.
One way to do it would be to make a copy of the List and then work over the original list like:
foreach (int i in list.ToList())
{
if (i == 2)
{
list.Add(666);
//do something to refresh the foreach loop
}
System.Diagnostics.Debug.Write(list[i]);
}
list.ToList
would make a copy of the list and the foreach
loop would work over it, thus saving you from the exception. But that is more like a hack, since you are not iterating the original list in the foreach
loop.
Upvotes: 4
Reputation: 15
Yes and no with the 2 answers already given.
There is a way to modify collection (List actually) in a foreach loop, by using the following syntax, from LINQ :
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
list.ForEach(elt => {
if(elt == 2) {
list.Add(666);
}
System.Diagnostics.Debug.Write(elt);
});
But be carefull... If you add "2" into your list within the condition, you'll go infinite :)
Upvotes: 0
Reputation: 171178
You could write yourself a custom enumerator or collection that allows you to modify the collection while enumerating. On the other hand, you probably should do something else entirely.
I'm not sure what you are trying to do, but this looks a lot better to me:
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
var outputList = new List<int>();
foreach (int i in list)
{
if (i == 2)
{
outputList.Add(666);
//do something to refresh the foreach loop
}
outputList.Add(list[i]);
}
Just create a fresh list. Try not to mutate data structures. That's rather fragile. It is usually better to create new data from old data.
Upvotes: 1
Reputation: 12805
You can't. The foreach
loop depends on the behavior of IEnumerable
with the result being that the collection can not be modified during the iteration of the items. You will get a Runtime Exception every time you attempt to perform those types of modification to the collection.
Upvotes: 1