tagged [loops]

How do I limit the number of elements iterated over in a foreach loop?

How do I limit the number of elements iterated over in a foreach loop? I have the following code But only want 6 items not all items, how can I do it in C#?

6 Jan at 16:43

Using lock statement within a loop in C#

Using lock statement within a loop in C# Lets take the sample class SomeThread where we are attempting to prevent the DoSomething methods from being called after the Running property is set to false a...

21 Jan at 21:59

Get a Try statement to loop around until correct value obtained

Get a Try statement to loop around until correct value obtained I am trying to get a user to enter a number between 1 and 4. I have code to check if the number is correct but I want the code to loop a...

11 Feb at 12:9

C# - How to create a non-detectable infinite loop?

C# - How to create a non-detectable infinite loop? This is just an "I am Curious" question. In C#-in-depth Jon Skeet says about lambda expressions: (Page 233) The footnote then says: (Page 233) I am w...

22 Feb at 19:5

C# foreach loop with key value

C# foreach loop with key value In PHP I can use a foreach loop such that I have access to both the key and value for example: I have the following code: ``` Regex regex = new Regex(pattern); MatchColl...

28 Feb at 10:19

Indexing my while loop with count parameter in an array

Indexing my while loop with count parameter in an array My function takes an array of ifstream ofjects and the number of ifstream objects as seen below: My problem is I want to use a while loop to rea...

4 Mar at 23:58

Loop Reversal in C# Speeds Up app

Loop Reversal in C# Speeds Up app We are working on a video processing application using EmguCV and recently had to do some pixel level operation. I initially wrote the loops to go across all the pixe...

5 Mar at 04:55

C# looping through an array

C# looping through an array I am looping through an array of strings, such as (1/12/1992 apple truck 12/10/10 orange bicycle). The array's length will always be divisible by 3. I need to loop through ...

9 Mar at 20:44

LINQ: How to skip one then take the rest of a sequence

LINQ: How to skip one then take the rest of a sequence i would like to iterate over the items of a `List`, except the first, preserving the order. Is there an elegant way to do it with LINQ using a st...

12 Mar at 09:47

C#: Nested conditionals vs continue statement

C#: Nested conditionals vs continue statement In using ReSharper recently, it is suggesting I reduce nesting in certain places by inverting `if` conditions and using the `continue` statements. ``` for...

26 Jul at 19:13

Are the limits of for loops calculated once or with each loop?

Are the limits of for loops calculated once or with each loop? Is the limit in the following loop (12332*324234) calculated once or every time the loop runs? ``` for(int i=0; i

31 Jul at 00:1

Problems removing elements from a list when iterating through the list

Problems removing elements from a list when iterating through the list I have a loop that iterates through elements in a list. I am required to remove elements from this list within the loop based on ...

23 Aug at 08:20

How do i exit a List<string>.ForEach loop when using an anonymous delegate?

How do i exit a List.ForEach loop when using an anonymous delegate? In a normal loop you can break out of a loop using break. Can the same be done using an anonymous delegate? Example inputString and ...

24 Aug at 08:31

How to loop on field names of a class

How to loop on field names of a class I have got a class which contains more then 150 fields. i need the name of fields (not value) in an array. because its very hard and not a good approach to write ...

10 Sep at 10:50

Simple check to see if at least one object in a set has a property value of TRUE

Simple check to see if at least one object in a set has a property value of TRUE I have a set of model objects that have a public `IsVisible` boolean property. All I need to do is find if at least one...

19 Sep at 07:15

C# getting all colors from Color

C# getting all colors from Color I want to make a `ComboBox` filled with all the colors from `System.Drawing.Color` But I can't seem to collect all the colors from that collection I've already tried u...

29 Sep at 11:30

Why is foreach loop Read-Only in C#

Why is foreach loop Read-Only in C# Why is foreach loop a read only? I mean you can fetch the data but can't increase++ or decrease--. Any reason behind it? Yes I am a beginner :) Exmaple:

23 Oct at 15:55

Is recursion ever faster than looping?

Is recursion ever faster than looping? I know that recursion is sometimes a lot cleaner than looping, and I'm not asking anything about when I should use recursion over iteration, I know there are lot...

25 Oct at 00:32

bash shell nested for loop

bash shell nested for loop I want to write a nested for loop that has to work in the bash shell prompt. nested for loop in Single line command. For example, In the above example, for loop is executed ...

31 Jan at 05:23

How do recursive function calls work in loops?

How do recursive function calls work in loops? I have a function, in which there is a loop which calls up the function. ``` function displayItem(item, isChild) { if (isChild) { writeOutput('' ...

23 Feb at 22:13

input multiple records into mysql db

input multiple records into mysql db i'm writing a script to throttle some of the domains i send my newsletter to. I've had issues with getting emails blocked in the past. All I'm doing is storing the...

16 Apr at 01:46

How can I loop through all rows of a table? (MySQL)

How can I loop through all rows of a table? (MySQL) I have a table A and there is one primary key ID. Now I want to go through all rows in A. I found something like 'for each record in A', but this se...

28 Apr at 11:19

Iterate over pixels of an image with emgu cv

Iterate over pixels of an image with emgu cv I want to iterate over all pixel of an image and compare with a search pattern. With best performance possible and in C#. I found emgu cv, a wrapper for In...

15 May at 12:13

Why does Lua have no "continue" statement?

Why does Lua have no "continue" statement? I have been dealing a lot with Lua in the past few months, and I really like most of the features but I'm still missing something among those: - `continue`-

25 May at 16:36

How to terminate outer loop in nested loops?

How to terminate outer loop in nested loops? What is the best way to terminate all nested loops in the example below. Once the if statement is true, I want to terminate the outer for statement (with I...

1 Jun at 12:27