C
C#2d ago
JoelQ

✅ Is the task using reference of the outer enumerable ?

Hi all, I would like to ask whether if the local variable is using the reference of the outer enumerable instead of copying and creating a new array?
var chunks = items.Chunks(1000);
foreach(var chunk in chunks)
{
var itemChunk = chunk;
Task.Run(() => {
Console.WriteLine(itemChunk.Length);
...
});
}
var chunks = items.Chunks(1000);
foreach(var chunk in chunks)
{
var itemChunk = chunk;
Task.Run(() => {
Console.WriteLine(itemChunk.Length);
...
});
}
3 Replies
ero
ero2d ago
Yes, it uses the reference
JoelQ
JoelQOP2d ago
Thank you @ero
canton7
canton72d ago
If it helps, there's no built-in way to copy in IEnumerable, and there's no way to implicitly copy any reference type

Did you find this page helpful?