title: How is such IndexOutOfRangeException possible when debugger shows the index is not out of range?
body: <p>This is the weirdest runtime error I ever got in C#:</p>
  <p>a)
  <a href="https://i.stack.imgur.com/vLH9w.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vLH9w.png" alt="enter image description here" /></a>
  b)
  <a href="https://i.stack.imgur.com/lb1Df.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lb1Df.png" alt="enter image description here" /></a></p>
  <p>This is ONE BREAKPOINT.
  You can see the <code>Messages</code> array has the length of 128.
  You can see AT THE SAME TIME the requested array index is 3 (or 2, I'm not sure if the index was incremented or not, but it should not matter here, index 3, length 128, so 0, 1, 2, 3 ... 127 should all be valid indices).</p>
  <p>How is the method called? A WebSocket client listens to messages. The messages are written to a circular buffer. The buffer is constant. Preallocated at the session start. So after offset 127 there is offset 0 and so on. It is important that the application should see the latest 127 messages so it can search for interesting stuff. This is a workaround for malformed content sent by the server. The server sends the message in wrong order. Let's say it sends state and updates. But in reverse order. First I get updates, then the state to which the updates are for. With that little trick it works. For hours and hours. And once upon a time my app dies. Being patient and leaving the app for a few hours under VS debugger I got this error. W T F ? !</p>
  <p>BTW, there is no concurrency here. There is only ONE session running in the process. The function as you can see is synchronous. There is one thread that reads the messages from the WS server.</p>
  <p>There are other threads that communicate to the clients what happens on the remote server, but it seems like the remote server reader have crashed. But what did just happen? The array is 128 items long. All the time. Created over an hour before the crash. The index is 2 or 3 - so within the valid range.</p>
  <p>The only explanation I can think of the VS debugger lies to me. Something else have crashed and the debugger shows me wrong part of the code. What now? How would you proceed to debug that stuff furtner?</p>
  <pre class="lang-cs prettyprint-override"><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
  namespace Woof.Crypto {
  
      class MessageBuffer {
  
          public int Capacity { get; }
  
          public long CurrentId { get; private set; }
  
  
          public MessageBuffer(int capacity = 128) {
              Messages = new (long, object)[Capacity = capacity];
          }
  
          public void Write(object message) {
              Messages[CurrentOffset++] = (CurrentId++, message);
              if (CurrentOffset &gt;= Capacity) CurrentOffset = 0;
          }
  
          public IEnumerable&lt;object&gt; GetFromId(long id) =&gt; Messages.Where(i =&gt; i.Item1 &gt; id).OrderBy(i =&gt; i.Item1).Select(i =&gt; i.Item2);
  
          readonly (long, object)[] Messages;
          int CurrentOffset;
  
      }
  
  }
  </code></pre>