I'm puzzled with a problem regarding C# List, the code below throws ArgumentOutOfRangeException:
List<int> l = new List<int>();
l.Add(1); l.Add(1); l.Add(1); l.Add(1); l.Add(1);
l.Add(1); l.Add(1); l.Add(1); l.Add(1); l.Add(1);
l.Add(1); l.Add(1); l.Add(1); l.Add(1); l.Add(1); // 15 elements
// v <--- From 0
l.FindLastIndex(0, 5, v => v != 1);
// ^ <--- up to 5 elements
As far as I understand the code above, the method will apply the lambda from the element 0 until it reaches 5 elements (element 4), but it throws the ArgumentOutOfRangeException even if it mustn't according to my understanding of the documentation:
ArgumentOutOfRangeException
startIndexis outside the range of valid indexes for theList<T>.-or-
countis less than 0.-or-
startIndexandcountdo not specify a valid section in theList<T>.
The most likely reason is the third one, but startIndex is 0 (inside the range) and count is far below l.Count so the section within the list is 0 to 4, which is valid.
What am I doing wrong and how to fix it?