All Questions
14
questions
-1
votes
2
answers
4k
views
Loop pattern for batch data processing
When processing data sets in batches I usually can think of the following three implementations.
Which one do you consider better than the other and why?
Notes:
The implementation is in C# but the ...
1
vote
0
answers
42
views
is it better to have tracking fields that are maintained separately for arrays? [duplicate]
I wasn't sure exactly how to word this question, but basically, I have a struct stNeuralLayers (for a neural network I'm playing around with) with fields that are matrices (such as a double[,] ...
-1
votes
4
answers
2k
views
Alternatives to Long Loops [closed]
I am part of a software team that is writing a console application (no UI) in C#. My part in the team is to write code to call RESTful APIs from a third party site, process the returned data and save ...
1
vote
1
answer
3k
views
What is the benefits of a foreach-loop vs a body-less for-loop
In C# you can use a for-loop without declaring the body, what is the benefit of using this in production-code versus using a foreach loop?
Example
My Examples below use reflection to get the ...
6
votes
7
answers
9k
views
While loop without evaluating data twice
I often come across the following pattern.
while(GetValue(i) != null)
{
DoSomethingWith(GetValue(i++));
}
Here GetValue is executed twice. It would be much nicer to use a pattern where we can ...
13
votes
2
answers
17k
views
What exactly happens when a thread awaits a task inside a while loop?
After dealing with C#'s async/await pattern for a while now, I suddenly came to realization that I don't really know how to explain what happens in the following code:
async void MyThread()
{
...
5
votes
3
answers
5k
views
Is implementing IEnumerable required to use foreach on collections?
I have the following class that doesn't implement IEnumerable but is working perfectly with foreach.
And also, arrays are working without implementing IEnumerable.
So why does it keep saying that ...
-2
votes
3
answers
8k
views
Best way to handle variables used in a for loop? [duplicate]
From previous experience, I had always thought that, if you are going to use variables inside of a for loop, it was much better to declare them outside of the loop vs. inside the loop itself. I ...
27
votes
2
answers
47k
views
Filtering foreach loops with a where condition vs continue guard clauses
I've seen some programmers use this:
foreach (var item in items)
{
if (item.Field != null)
continue;
if (item.State != ItemStates.Deleted)
continue;
// code
}
instead ...
0
votes
2
answers
3k
views
Loops to create nested loops
Is there a way to use a loop to create more nested loops? E.g. Doing this
for (int i = 0; i < iterations; i++)
{
//Do stuff
for (int ii = 0; ii < ...
2
votes
2
answers
290
views
How to iterate between these elements
I have an array of elements:
int[] elem = new int[] {A, B, C};
I need to calculate the sum of ALL the combinations of those elements, where only some of the elements can be optionally selected. I ...
2
votes
2
answers
128k
views
foreach in list or foreach in list.where [duplicate]
I don't know what to call this question.
This is my example:
foreach (var item in lstItem.Where(item => stock.ItemCode == item.ItemCode))
{
stock.ItemName = item.ItemName;
stock....
4
votes
4
answers
7k
views
Decrementing/Incrementing loop variable inside for loop. Is this code smell?
I have to read lines from a text file in sequential order. The file is a custom text format that contains sections. If some sections are out of order, I would like to look for the starting of the next ...
9
votes
8
answers
11k
views
C# foreach improvements?
I run into this often during programming where I want to have a loop count index inside of a foreach and have to create an integer, use it, increment, etc. Wouldn't it be a good idea if there was a ...