System.IndexOutOfRangeException on the collection which initialized once and length not changing, how?
<p>I can not understand why I received today <code>System.IndexOutOfRangeException</code>. I've received it when program was working for 8 hours already and the place where exception occurs was executed many millions times. This place is very simple:</p>

<pre><code>for (int i = 0; i &lt; _goldDesiredOrdersBuy.Length; i++)
{
    _goldDesiredOrdersBuy[i] = -1;               // IndexOutOfRangeException! Strategy3.cs:line 666
}
</code></pre>

<p>I initialize _goldDesiredOrdersBuy only once when program starts so it's guaranteed that it's initialized when exception occured:</p>

<pre><code>private int[] _goldDesiredOrdersBuy = new int[MaxOrderbookDepth];
</code></pre>

<p>I have one another place where I touch this array:</p>

<pre><code>    private int GetGoldVolumeBuy(int bidQuotesPos)
    {
        if (_goldDesiredOrdersBuy[bidQuotesPos] &gt; -1)
        {
            return _goldDesiredOrdersBuy[bidQuotesPos];
        }
        int result = GetGoldVolumeBuyNotCached(bidQuotesPos);
        _goldDesiredOrdersBuy[bidQuotesPos] = result;
        return result;
    }
</code></pre>

<p>And that's all. <code>_goldDesiredOrdersBuy</code> initialized once when application starts, guranteed to be initialized and length of array is not modifing anywhere, so I don't understand how I received <code>IndexOutOfRangeException</code>, any thoughts?</p>

<pre><code>Unhandled Exception: System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. ---&gt;
   System.AggregateException: One or more errors occurred. ---&gt;
   System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at MyProj.Strategies.Strategy3.CalculateNewDesiredOrdersBuy() in C:\Oleg\projects\MyProj\MyProj\Strategies\Strategy3.cs:line 666
   at MyProj.Strategies.Strategy3.RecalculateBuyOrders() in C:\Oleg\projects\MyProj\MyProj\Strategies\Strategy3.cs:line 567
   at MyProj.Strategies.Strategy3.OnAllTablesUpdated() in C:\Oleg\projects\MyProj\MyProj\Strategies\Strategy3.cs:line 499
   at MyProj.Strategies.Strategy3.AllTablesUpdated() in C:\Oleg\projects\MyProj\MyProj\Strategies\Strategy3.cs:line 413
   at System.Threading.Tasks.Parallel.&lt;&gt;c__DisplayClassf`1.&lt;ForWorker&gt;b__c()
   at System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask)
   at System.Threading.Tasks.Task.&lt;&gt;c__DisplayClass7.&lt;ExecuteSelfReplicating&gt;b__6(Object )
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Func`4 bodyWithLocal, Func`1 localInit, Action`1 localFinally)
   at System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable`1 source, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally)
   at System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable`1 source, Action`1 body)
   at MyProj.Market.FinishUpdatingTables() in C:\Oleg\projects\MyProj\MyProj\Market.cs:line 449
   at Library.Exchange.Gate.DoGateIteration() in C:\Oleg\projects\MyProj\MyProj\Gate.cs:line 143
   at Library.Exchange.Gate.&lt;Connect&gt;b__0() in C:\Oleg\projects\MyProj\MyProj\Gate.cs:line 98
   at System.Threading.Tasks.Task.Execute()
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.TaskExceptionHolder.Finalize()
</code></pre>
