I have a while loop wherein I do something with a memory stream - namely passing it to other objects that fill the stream or read from it. The code looks something like this:
public async void CaptureImages(CancellationToken ct)
{
while(!ct.IsCancellationRequested)
{
await using var memoryStream = new MemoryStream();
await this.camera.CaptureImage(memoryStream, ct);
await this.storage.StoreImage(memoryStream, ct);
}
}
My question is: Will memoryStream
be disposed in every iteration or once the loop ends?
While the question C# 8 Using Declaration Scope Confusion answers this topic generally, it does not explicitly answer the question about the scope of disposable variables in a while-loop.