tagged [foreach]

How to 'foreach' a column in a DataTable using C#?

How to 'foreach' a column in a DataTable using C#? How do I loop through each column in a datarow using `foreach`?

11 Sep at 19:0

How does the Java 'for each' loop work?

How does the Java 'for each' loop work? Consider: What would the equivalent `for` loop look like without using the syntax?

23 Feb at 13:21

Is the order of objects returned by FOREACH stable?

Is the order of objects returned by FOREACH stable? Is it safe to assume that two itterations over the same collection will return the objects in the same order? Obviously, it is assumed that the coll...

C# Break out of foreach loop after X number of items

C# Break out of foreach loop after X number of items In my foreach loop I would like to stop after 50 items, how would you break out of this foreach loop when I reach the 50th item? Thanks

11 Aug at 22:36

Parallel.Foreach exceptions and cancel

Parallel.Foreach exceptions and cancel I have tried to find out how exceptions and cancel work for `Parallel.Foreach`. All examples seems to deal with Tasks. What happens on an exception in `Parallel....

C#: Any benefit of List<T>.ForEach(...) over plain foreach loop?

C#: Any benefit of List.ForEach(...) over plain foreach loop? I'm wondering why `List.ForEach(Action)` exists. Is there any benefit/difference in doing : over ?

17 Dec at 22:17

ForEach to Trim string values in string array

ForEach to Trim string values in string array I just wondered why this ForEach doesn't work and leaves the values with trailing whitespace.

15 Feb at 13:15

Short circuit Array.forEach like calling break

Short circuit Array.forEach like calling break How can I do this using the new `forEach` method in JavaScript? I've tried `return;`, `return false;` and `break`. `break` crashes and `return` does noth...

28 Oct at 09:21

Should I always use Parallel.Foreach because more threads MUST speed up everything?

Should I always use Parallel.Foreach because more threads MUST speed up everything? Does it make sense to you to use for every normal foreach a parallel.foreach loop ? When should I start using parall...

c# string.replace in foreach loop

c# string.replace in foreach loop Somehow I can't seem to get string replacement within a foreach loop in C# to work. My code is as follows : Am still quite new to LINQ so pardon me if this sounds ama...

26 Apr at 10:4

change values in array when doing foreach

change values in array when doing foreach example: The array is still with it's original values, is there any way to have writing access to array's elements from iterating function ?

Does foreach() iterate by reference?

Does foreach() iterate by reference? Consider this: Is `obj` a reference to the corresponding object within the list so that when I change the property the change will persist in the object instance o...

30 Sep at 09:0

What is the difference between for and foreach?

What is the difference between for and foreach? What is the major difference between `for` and `foreach` loops? In which scenarios can we use `for` and not `foreach` and vice versa. Would it be possib...

16 Dec at 05:22

foreach loop with a where clause

foreach loop with a where clause I was wondering if it's possible to create a foreach loop in `C#` with a where loop. Not with a if statement inside, but and where clause in the declaring of the loop....

4 Feb at 09:25

JSON forEach get Key and Value

JSON forEach get Key and Value I have the following `forEach` loop over a JSON object called `obj`: How can I make it `console.log` both `key` and `value` of each item inside the object? Something lik...

3 Dec at 15:18

How do I apply the for-each loop to every character in a String?

How do I apply the for-each loop to every character in a String? So I want to iterate for each character in a string. So I thought: but I get a compiler error: How can I do this?

23 Feb at 22:49

ForEach() : Why can't use break/continue inside

ForEach() : Why can't use break/continue inside Since ForEach() method loop through all list members, Why can't I use a clause while I can use them inside a normal foreach loop Error: > "No enclosing ...

19 Dec at 19:34

Performance difference for control structures 'for' and 'foreach' in C#

Performance difference for control structures 'for' and 'foreach' in C# Which code snippet will give better performance? The below code segments were written in C#. 1. ``` for(int tempCount=0;tempCoun...

20 Jan at 23:8

What does the colon (:) operator do?

What does the colon (:) operator do? Apparently a colon is used in multiple ways in Java. Would anyone mind explaining what it does? For instance here: ``` String cardString = ""; for (PlayingCard c :...

19 Jan at 15:15

How do I jump out of a foreach loop in C#?

How do I jump out of a foreach loop in C#? How do I break out of a `foreach` loop in C# if one of the elements meets the requirement? For example:

7 Aug at 07:36

foreach day in month

foreach day in month > [How do I loop through a date range?](https://stackoverflow.com/questions/1847580/how-do-i-loop-through-a-date-range) Is there a way to make a foreach loop for each day in a s...

23 May at 11:46

How do I exit a foreach loop in C#?

How do I exit a foreach loop in C#? Whenever violated is true, I want to get o

13 Mar at 14:45

How does foreach work when looping through function results?

How does foreach work when looping through function results? Suppose I have the following code: Will `someObj.GetMyStrings()` be called on every iteration of the loop? Would it be better to do the fol...

27 Oct at 18:36

Is there an equivalent to 'continue' in a Parallel.ForEach?

Is there an equivalent to 'continue' in a Parallel.ForEach? I am porting some code to `Parallel.ForEach` and got an error with a `continue` I have in the code. Is there something equivalent I can use ...

20 Mar at 18:10

Does Class need to implement IEnumerable to use Foreach

Does Class need to implement IEnumerable to use Foreach This is in C#, I have a class that I am using from some else's DLL. It does not implement IEnumerable but has 2 methods that pass back a IEnumer...

24 Sep at 13:52