You probably know this already but I just wanted to point out that the LoopIndex method fails when given an index that is less than -List.Count.
For example this fails:
// fails
int count = 360;
int index = -361;
int invalidIndex = (index + count) % count;
Console.WriteLine(invalidIndex); // writes -1
Here is a solution that preserves the original function output but also supports indexes less than -List.Count
// solution
int count = 360;
int index = -361;
int i = index % count;
if ((i < 0 && count > 0) || (i > 0 && count < 0)) i = i + count;
Console.WriteLine(i); // writes 359
The solution only performs i+count when the signs of i and count are different.
You probably know this already but I just wanted to point out that the LoopIndex method fails when given an index that is less than -List.Count.
For example this fails:
Here is a solution that preserves the original function output but also supports indexes less than -List.Count
The solution only performs
i+countwhen the signs ofiandcountare different.