It would be nice to (optionally) have access to ordinal numbers in foreach loops.
For example, printing an enumerated list of names:
The keyword 'ord' starts at 0 and increments by 1 per iteration. It requires an alias for resolving ambiguity in nested foreach loops.
Use of 'ord' is known at compile time, so no concerns about extra overhead in foreach loops that don't reference it.
Also, using 'ord' for array initialization:
Instead of:
For example, printing an enumerated list of names:
C#:
foreach (string name in names ord as n)
Debug.Log(string.Format("{0}. {1}", n + 1, name));
The keyword 'ord' starts at 0 and increments by 1 per iteration. It requires an alias for resolving ambiguity in nested foreach loops.
Use of 'ord' is known at compile time, so no concerns about extra overhead in foreach loops that don't reference it.
Also, using 'ord' for array initialization:
C#:
int[] ints = int[12];
foreach (int in ints ord as n) // initialize to {1..12}
value = n + 1; // value is write-only here
Instead of:
C#:
int[] ints = int[12];
for (int i = 0; i < ints.Length; i++) // initialize to {1..12}
ints[i] = i + 1;
Last edited: