I was just shocked when my application fired an IndexOutOfRanage exception now. I opened the Debugger Locals Pane and discovered that my integer crossed it’s boundary? Basically I have something like this in my code:
string folder = Extender.GetSetting<string>("textFolder");
string mlink = folder + "\\" + filename + ".txt";
if(File.Exists(mlink))
{
string fContent = File.ReadAllText(mlink);
rtbLearnGuide.Text = fContent;
string[] strings = fContent.Split(' ');
for (int i = 0; i < strings.Length; i++, words.Enqueue(strings[i]));
}
The problem here is that i reaches the length of strings[], I have attached a picture below.

What’s even more weird is that I failed to reproduce this behavior a second time.
NB: I experienced something similar earlier today with this.CreateGraphics(); My code was something like:
var dc = this.CreateGraphics();//and some other stuff
The result was that it failed to draw it even after trying to rerun like 4 times, then I went back to the code and defined dc explicitly, voila it was working. Then I changed it back to var, it was still working :/?
What might be wrong?
EDIT:
I just discovered that changing the order works. For instance:
for (int i = 0; i < strings.Length; words.Enqueue(strings[i]), i++);
doesn’t fire any errors.
To answer the first part of your question, you are executing the Enqueue before the for loop’s test condition. So
should be:
Basically, the “increment” portion will always execute before the “test” portion.