Memory Streams to Close() or Dispose()

I was at the .net users group meeting the other night and we were discussing code analysis.  Just for fun I ran it on the project I was working on that day and ran across this warning. 

Dispose Warning

Here is (basically) the code that was causing it.

Code Sample

I asked, why is this an issue?  A couple of comments.

  • Are you disposing of it in a using statement?
  • Are you doing this in a try block and disposing of it in both the try and the finally?

Neither of those is happening. 

The generally consensus was it should not do that because close never calls dispose, but after some research I see why it is.  Since code analysis works on the IL code it can see things we can’t see on the surface.  According to this answer on stack overflow the close method is calling dispose for you.  That’s handy right?  How about this from the MSDN documentation, emphasis mine.

Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream. Instead of calling this method, ensure that the stream is properly disposed.

That seems reasonable, apart from having a public method I am told in the docs not to call.  Going too far the other way removing both the .Close() and .Dispose() leaves us with this message. 

No Dispose Warning

I am going to go with just .Dispose().  Today its not really different from calling both, but in the future it might be and the docs say to do it this way.  Plus, it gets rid of the nagging warning.