tagged [foreach]

What's the fastest way to copy the values and keys from one dictionary into another in C#?

What's the fastest way to copy the values and keys from one dictionary into another in C#? There doesn't seem to be a dictionary.AddRange() method. Does anyone know a better way to copy the items to a...

18 Sep at 08:24

Linq style "For Each"

Linq style "For Each" Is there any Linq style syntax for "For each" operations? For instance, add values based on one collection to another, already existing one: Instead of

31 Jan at 07:11

break out of if and foreach

break out of if and foreach I have a foreach loop and an if statement. If a match is found i need to ultimately break out of the foreach.

13 Jul at 14:34

Cannot assign void to an implicitly-typed local variable with var and foreach

Cannot assign void to an implicitly-typed local variable with var and foreach I'm trying to list all buttons name from my form to list with code and always get error > Cannot assign void to an implici...

2 Apr at 14:17

how do I check if an entity is the first element of a foreach loop

how do I check if an entity is the first element of a foreach loop Say I have a `foreach` loop. I have to do something with the first object of the loop that I don't have to do with any of the other o...

25 Mar at 05:20

c# exit generic ForEach that use lambda

c# exit generic ForEach that use lambda Does anyone know if it is possible to exit a generic ForEach that uses lambda? e.g. This code itself won't compile. I know I could use a regular foreach but for...

12 Feb at 03:9

Is a LINQ statement faster than a 'foreach' loop?

Is a LINQ statement faster than a 'foreach' loop? I am writing a Mesh Rendering manager and thought it would be a good idea to group all of the meshes which use the same shader and then render these w...

29 Aug at 15:29

Combining foreach and using

Combining foreach and using I'm iterating over a ManageObjectCollection.( which is part of WMI interface). However the important thing is, the following line of code. : The point is that ManageObject ...

9 Jun at 11:32

Iterate Multi-Dimensional Array with Nested Foreach Statement

Iterate Multi-Dimensional Array with Nested Foreach Statement I think this might be a pretty simple question, but I haven't been able to figure it out yet. If I've got a 2-dimensional array like so: W...

12 Sep at 12:1

Operator '??' cannot be applied to operands of type 'System.DateTime'

Operator '??' cannot be applied to operands of type 'System.DateTime' I get the following error : --- ``` foreach (EndServReward r in reward) { if (con.State == Connectio...

How do you get the index of the current iteration of a foreach loop?

How do you get the index of the current iteration of a foreach loop? Is there some rare language construct I haven't encountered (like the few I've learned recently, some on Stack Overflow) in C# to g...

23 Mar at 08:51

Foreach loop, determine which is the last iteration of the loop

Foreach loop, determine which is the last iteration of the loop I have a `foreach` loop and need to execute some logic when the last item is chosen from the `List`, e.g.: Can I know which loop is last...

19 Sep at 19:34

Is there a way to iterate over a dictionary?

Is there a way to iterate over a dictionary? I know `NSDictionaries` as something where you need a `key` in order to get a `value`. But how can I iterate over all `keys` and `values` in a `NSDictionar...

How can I access the next value in a collection inside a foreach loop in C#?

How can I access the next value in a collection inside a foreach loop in C#? I'm working in C# and with a sorted `List` of structs. I'm trying to iterate through the `List` and for each iteration I'd ...

9 Mar at 19:7

Why can't I set properties of iteration variables in a foreach loop?

Why can't I set properties of iteration variables in a foreach loop? I expected the above line to return true. W

3 Feb at 02:50

c# parallel foreach loop finding index

c# parallel foreach loop finding index I am trying to read all lines in a text file and planning to display each line info. How can I find the index for each item inside loop? ``` string[] lines = Fil...

21 Feb at 16:33

Get current index from foreach loop

Get current index from foreach loop Using C# and Silverlight How do I get the index of the current item in the list? Code: ``` IEnumerable list = DataGridDetail.ItemsSource as IEnumerable; List lstFil...

27 Apr at 08:9

How do I pass 2 lists into Parallel.ForEach?

How do I pass 2 lists into Parallel.ForEach? How do I pass 2 lists into `Parallel.ForEach`? Example: I would prefer to avoid encapsulating P

15 Jan at 11:32

Does the foreach loop in C# guarantee an order of evaluation?

Does the foreach loop in C# guarantee an order of evaluation? Logically, one would think that the foreach loop in C# would evaluate in the same order as an incrementing for loop. Experimentally, it do...

2 Feb at 14:42

Debugging a foreach loop in C#: what iteration is this?

Debugging a foreach loop in C#: what iteration is this? Other than setting a debug variable and incrementing it every time you start the foreach, when you break in with the Visual Studio debugger conn...

Overload indexer to have foreach'able class

Overload indexer to have foreach'able class I tried to do something like this but this doesn't work: ``` class Garage { private List cars = new List(); public Car this[int i] { get...

22 Sep at 12:24

How does the enhanced for statement work for arrays, and how to get an iterator for an array?

How does the enhanced for statement work for arrays, and how to get an iterator for an array? Given the following code snippet: I have the following questions: 1. How does the above for-each loop work...

26 Jul at 14:16

How is foreach implemented in C#?

How is foreach implemented in C#? How exactly is `foreach` implemented in C#? I imagine a part of it looking like: However I'm unsure what's really going on. What methodology is used for returning `en...

Conditional C# breakpoint?

Conditional C# breakpoint? I'm debugging a `foreach` loop which will iterate well over 1000 times - so I only want a breakpoint within the loop to break for a particular item. So... Do I have to write...

30 Apr at 16:12

What is the for/while equivalent of foreach?

What is the for/while equivalent of foreach? I have a `foreach` loop that needs converting to a `for` or `while` loop. My loop looks like this: What is the equivalent `for` or `while` loop? I think I ...

How can I convert this foreach code to Parallel.ForEach?

How can I convert this foreach code to Parallel.ForEach? I am a bit of confused about `Parallel.ForEach`. What is `Parallel.ForEach` and what does it exactly do? Please don't reference any MSDN link. ...

Going Through A Foreach When It Can Get Modified?

Going Through A Foreach When It Can Get Modified? I want to do a foreach loop while taking out members of that foreach loop, but that's throwing errors. My only idea is to create another list inside o...

12 Jul at 21:41

asp.net mvc razor foreach loop adding id to div

asp.net mvc razor foreach loop adding id to div I am trying to add dynamic `id` to `div` inside a `foreach` loop concatenated with value of variable `i`. It throws syntax errors. What might be the iss...

23 Jan at 07:21

Go to "next" iteration in JavaScript forEach loop

Go to "next" iteration in JavaScript forEach loop How do I go to the next iteration of a JavaScript `Array.forEach()` loop? For example: [MDN docs](https://developer.mozilla.org/en-US/docs/Web/JavaScr...

11 Aug at 22:45

How to modify a foreach iteration variable from within foreach loop?

How to modify a foreach iteration variable from within foreach loop? When I try to do this... ...I get `Cannot assign to 'item' because it is a 'foreach iteration variable'`. Still, I'd

Is it possible for a 'foreach' loop to have a condition check?

Is it possible for a 'foreach' loop to have a condition check? If I have a `foreach` loop, is there any way to check a boolean as well? I don't want to check once inside the `foreach()` and then break...

13 Jun at 17:41

Java foreach loop: for (Integer i : list) { ... }

Java foreach loop: for (Integer i : list) { ... } When I use JDK5 like below on the other hand if I just use an `Iterator` ``` ArrayList list = new ArrayList(); for (Iterator i = list.iterator(); i.h...

7 Nov at 16:31

Why are assignment operators (=) invalid in a foreach loop?

Why are assignment operators (=) invalid in a foreach loop? Why are assignment operators (=) invalid in a `foreach` loop? I'm using C#, but I would assume that the argument is the same for other langu...

23 Aug at 16:55

Is it possible to change parallelOptions.MaxDegreeOfParallelism during execution of a Parallel.ForEach?

Is it possible to change parallelOptions.MaxDegreeOfParallelism during execution of a Parallel.ForEach? I am running a multi-threaded loop: I want to change the `parallelOptions.MaxDegreeOfParallelism

Parallel.ForEach vs Task.Run and Task.WhenAll

Parallel.ForEach vs Task.Run and Task.WhenAll What are the differences between using `Parallel.ForEach` or `Task.Run()` to start a set of tasks asynchronously? Version 1: Version 2: ``` List strings =...

How does a queue interact with a foreach loop

How does a queue interact with a foreach loop I was really suprised that this question doesn't really have an answer. For example I have a foreach loop iterating through the queue(I am using the .Net ...

12 Oct at 14:36

Calling remove in foreach loop in Java

Calling remove in foreach loop in Java In Java, is it legal to call remove on a collection when iterating through the collection using a foreach loop? For instance: As an addendum, is it legal to remo...

30 Jul at 20:16

Less-verbose way of handling the first pass through a foreach?

Less-verbose way of handling the first pass through a foreach? I often find myself doing the following in a foreach loop to find out if I am on the or not. Is there a way to do this in , something alo...

4 Sep at 23:55

Parallel.ForEach loop with BlockingCollection.GetConsumableEnumerable

Parallel.ForEach loop with BlockingCollection.GetConsumableEnumerable Why `Parallel.ForEach` loop exits with `OperationCancelledException`, while using `GetConsumableEnumerable`? ``` //outside the fun...

Parallel.ForEach() vs. foreach(IEnumerable<T>.AsParallel())

Parallel.ForEach() vs. foreach(IEnumerable.AsParallel()) Erg, I'm trying to find these two methods in the BCL using Reflector, but can't locate them. What's the difference between these two snippets? ...

C# - Using foreach to loop through method arguments

C# - Using foreach to loop through method arguments Is it possible to loop through a function arguments to check if any of them is null(or check them by another custom function)? something like this: ...

24 Jul at 13:42

Why loop on array object with `foreach` is faster than lambda `ForEach`?

Why loop on array object with `foreach` is faster than lambda `ForEach`? I work on an array that and I have to loop over it. First, I use lambda `ForEach` and then I use simple `foreach`. I find that ...

27 May at 04:31

How to get a index value from foreach loop in jstl

How to get a index value from foreach loop in jstl I have a value set in the `request` object like the following, and this is how I iterate in jsp page ```

14 Mar at 19:13

How to use forEach in vueJs?

How to use forEach in vueJs? I have a response like below from an API call, [](https://i.stack.imgur.com/lMr5I.png) Now, I have to repeat the whole arrays inside the arrays. How do I do that in VueJS?...

Thoughts on foreach with Enumerable.Range vs traditional for loop

Thoughts on foreach with Enumerable.Range vs traditional for loop In C# 3.0, I'm liking this style: over the traditional `for` loop: ``` // Write the numbers 1 thru 7 for (int index = 1; index

17 Oct at 16:38

Removing XElements in a foreach loop

Removing XElements in a foreach loop So, I have a bug to remove The problem is that calling x.Remove() alters the foreach such that if there are two Elements("x"), and the first is removed, the loop d...

16 Oct at 19:45

Iterating over result of getElementsByClassName using Array.forEach

Iterating over result of getElementsByClassName using Array.forEach I want to iterate over some DOM elements, I'm doing this: but I get an error: > document.getElementsByClassName("myclass").forEach i...

How can I limit Parallel.ForEach?

How can I limit Parallel.ForEach? I have a Parallel.ForEach() async loop with which I download some webpages. My bandwidth is limited so I can download only x pages per time but Parallel.ForEach execu...

29 Aug at 21:26

Is foreach purely “syntactic sugar”?

Is foreach purely “syntactic sugar”? The compiler compiles a `foreach` loop into something like a `for` loop when the `foreach` is used with an array. And the compiler compiles a `foreach` loop into s...

Is there a way to access an iteration-counter in Java's for-each loop?

Is there a way to access an iteration-counter in Java's for-each loop? Is there a way in Java's for-each loop to find out how often the loop has already been processed? Aside from using the old and we...

27 Jan at 12:28