I'm currently using two objects as follows:
using (var ms = new MemoryStream())
using (var bw = new BinaryWriter(ms))
{
// work with ms and bw, both referenced here
}
It works "fine" and is in fact an answer here too. However, when I run VS2012's code analysis tool, I get a warning like:
CA2202 Do not dispose objects multiple times
Object 'ms' can be disposed more than once in method '<my method name>'.
To avoid generating a System.ObjectDisposedException you should not
call Dispose more than one time on an object.
This leads me to believe there might an alternative way to handle this situation, but I don't know what it is.
Does anyone know what is the 'proper' way to use two objects within a single using
block in a warning-free manner?