Can I assume the following code will always pass my assertions? I'm a worried about the index value. I'm not sure if the scoped value will be passed along to the Task.Run lambda expression. I think it will be scoped just like the attributesChunked value seems to be. But I'd like some confirmation.
var tasks = new List<Task>();
var attributes = new string[4]{ "att1", "att2", "att3", "att4" };
var chunkIndex = -1;
foreach (var attributesChunked in attributes.Chunk(2))
{
var index = Interlocked.Increment(ref chunkIndex);
tasks.Add(Task.Run(() =>
{
if (index == 0)
Assert.Equal("attr1", attributesChunked.First());
if (index == 2)
Assert.Equal("attr3", attributesChunked.First());
}
}
await Task.WhenAll(tasks);