-2

I recently used a Parallel.For loop (see code below) in a small C# program and I'm a little perplexed by the ctr variable. Every example I've seen so far has the name of this variable set to ctr, but I cant seem to find any good resource on what it means or why exactly this name is used.

If anyone knows more about it, I would be happy to hear it!

public static int[] calcArray(int[] arrName, int arrSize, int seed)
{
    Parallel.For(0, arrSize, ctr =>
   {
       arrName[ctr] = AllfunctionsClass.Random(seed);
       seed++;
   });
    return arrName;
}
Zelyson
  • 9
  • 1
  • 3
    As far as I know it stands for "counter". It's just like "i" in a generic for loop. You can call it what you want. – b0neng4 May 15 '21 at 14:39
  • [Lambda expressions (C# reference)](https://learn.microsoft.com/dotnet/csharp/language-reference/operators/lambda-expressions): "*To create a lambda expression, you specify input parameters (if any) on the left side of the lambda operator and an expression or a statement block on the other side.*" Here it is the same as an int index of a loop for: [Parallel.For](https://learn.microsoft.com/dotnet/api/system.threading.tasks.parallel.for) –  May 15 '21 at 14:45

1 Answers1

3

The current index value.. imagine a normal for loop

for (int ctr=0; ctr < arraySize; ctr++)
{
    // ctr is the current value between 0 and arraySize-1
}

The variable name chosen as arbitrary in this case, probably short for counter. IMHO variable names should very rarely be abbrvted and should make it obvious what they represent e.g. arrayPosition or position or maybe index or something like that

bwakabats
  • 603
  • 3
  • 9