I have the follow piece of code that loop through an array, where the array element stores a file name. Per loop, the code load an XML document. When I call the array element directly I receive an exception that says index was outside the boundary of array, but if I store the array element to a separate variable, the code compile just fine.
I just cannot understand why there is a difference, and why one work but not the other.
Compile OK
for(int i =0; i < MyArray.Count(); i++)
{
string myString = MyArray[i].Split(',')[0];
Dispatcher.BeginInvoke(new Action(() =>
{
string xmlPath = _PATH + + myString;
var document = XDocument.Load(xmlPath, LoadOptions.SetLineInfo);
}));
}
}
Exception: Index was outside the boundary of array
for(int i =0; i < MyArray.Count(); i++)
{
Dispatcher.BeginInvoke(new Action(() =>
{
string xmlPath = _PATH + + MyArray[i].Split(',')[0];
var document = XDocument.Load(xmlPath, LoadOptions.SetLineInfo);
}));
}
}
The issue is caused by "variable capture", it uses the final value of
i
not the value at the time you called it. To fix the problem make a local variable inside of the loop and use that instead.