ARTICLE AD BOX
I need to process a list of FileStream objects with the DoSomethingWithFileStreams method like below. If I write code like this, I am concerned that the list of FileStream gets disposed before they are processed in the DoSomethingWithFileStreams method.
List<Stream> fileStreams = new List<Stream>(); foreach (var attachmentPath in attachmentPaths) { using (FileStream fileStream = new FileStream( attachmentPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { fileStreams.Add(fileStream); } } DoSomethingWithFileStreams(fileStreams);Should I be writing like below? In this new version, I am not using the using statement and disposing FileStream with the Dispose method.
List<Stream> fileStreams = new List<Stream>(); foreach (var attachmentPath in attachmentPaths) { FileStream fileStream = new FileStream( attachmentPath, FileMode.Open, FileAccess.Read, FileShare.Read); fileStreams.Add(fileStream); } DoSomethingWithFileStreams(fileStreams); foreach (var ifileStream in fileStreams) { ifileStream.Dispose(); }