List.Add() Index Was Out Of Bounds Of The Array
I've been working on some bulk processes lately, and this error popped up on occasion, which at first glance was quite confusing - but a quick search online taught me something which I completely overlooked: List<T>
is not thread safe.
So if you're using Parallel.For()
like me or just using threads in general, if you don't want to handle synchronization yourself, consider using one of the thread-safe collections. I went with ConcurrentBag<T>
, but there are a few to choose from depending on what you're trying to achieve.