I have a List<Point> that is supposed to be cleared often, as the values are used once per iteration, and I get an ArgumentOutOfRangeException while invoking the Clear() method.
I read a explanation that deals with multi-threading, but as far as I’m aware I’ve created no threads (unless c# automates for or while loops into threads somehow, but I doubt that).
Here is my code:
List<Point> temp = new List<Point>();
List<Point> visited = new List<Point>();
// other initializations
for(int i = 0; i < points.Count;i++){
if(temp.Count != 0)
temp.Clear(); // Problem occurs here
temp.Add(A);
temp.Add(B);
temp.Add(C);
temp.Add(D);
while(!(visited.Contains(temp[0]) && visited.Contains(temp[1])...){
// calculate some stuff
for(int j = 0; j <4;j++)
visited.Add(temp[j]);
// use point of interests
temp.Clear(); // no issue on this clear
temp.Add(newA);
temp.Add(newB);
temp.Add(newC);
temp.Add(newD);
}
// display results
}
It throws the ArgumentOutOfRangeException from the first temp.Clear() call.
I’ve also noticed that if I clear the visited list in the same position as the problem temp.Clear(), I get the same error.
To me it seems like a problem that is c# specific, and as I’m very new to the language I’m wondering if I’m not understanding something.
I think your exception is when you try to access your
tempitems, List.Clear() doesn’t throwArgumentOutOfRangeExceptionbecause implements first IList.Clear() and finally ICollection.Clear(), and they just throwNotSupportedExceptionfrom .net2 till now notArgumentOutOfRangeException, If you call your method in different threads I guess your problem is within this lines:and you can solve it by surrounding this part in
lockblock.Also it’s better to use
visited.Intersect(temp).Count ==0instead of your longifcondition.Edit: By your updated question problem is clear:
while(… (current != points[i + 1])) ooopps
when i = n – 1,
points[i + 1]is out of range.