The new Visual Studio 2012 is complaining about a common code combination I have always used. I know it seems like overkill but I have done the following in my code 'just to be sure'.
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (var sr = new StreamReader(fs))
{
// Code here
}
}
Visual studio is 'warning' me that I am disposing of fs more than once. So my question is this, would the proper way to write this be:
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
var sr = new StreamReader(fs);
// do stuff here
}
Or should I do it this way (or some other variant not mentioned).
var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using (var sr = new StreamReader(fs))
{
// Code here
}
I searched several questions in StackOverflow but did not find something that addressed the best practice for this combination directly.
Thank you!
Given all the nonsense this (perfectly legitimate!) question generated, this would be my preference:
The links below illustrate perfectly legal syntax. IMO, this "using" syntax is far preferable to nested "using". But I admit - it does not solve the original question:
http://blogs.msdn.com/b/ericgu/archive/2004/08/05/209267.aspx
.NET - Replacing nested using statements with single using statement
IMHO...
As Dan's answer only appears to work with StreamWriter, I believe this might be the most acceptable answer. (Dan's answer will still give the disposed twice warning with StreamReader - as Daniel Hilgarth and exacerbatedexpert mentions, StreamReader disposes the filestream)
This is very similar to Daniel Hilgarth's answer, modified to call dispose via the Using statement on StreamReader as it is now clear StreamReader will call dispose on FileStream (According to all the other posts, documentation referenced)
Update:
I found this post. For what it is worth. Does disposing streamreader close the stream?
Two solutions:
A) You trust Reflector or Documentation and you know
*Reader
and*Writer
will close the underlying*Stream
. But warning: it won't work in case of a thrown Exception. So it is not the recommended way:B) You ignore the warning as documentation states
The object must not throw an exception if its Dispose method is called multiple times.
It's the recommended way, as it's both a good practice to always useusing
, and safe in case of a thrown Exception:Yes, the correct way would be to use your first alternative:
The reason is the following:
Disposing the
StreamReader
only disposes theFileStream
so that's actually the only thing you need to dispose.Your second option (just the inner "using") is no solution as it would leave the
FileStream
undisposed if there was an exception inside the constructor of theStreamReader
.The following is how Microsoft recommends doing it. It is long and bulky, but safe:
This method will always ensure that everything is disposed that should be despite what exceptions may be thrown. For example, if the
StreamReader
constructor throws an exception, theFileStream
would still be properly disposed.You are, but that is fine. The documentation for IDisposable.Dispose reads:
Based on that, the warning is bogus, and my choice would be to leave the code as it is, and suppress the warning.