What is the 'ctr' variable in a parallel C# loop?

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);
    return arrName;

1 answer

  • answered 2021-05-15 14:43 bwakabats

    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