c# - Multiple variables within same using block -
this question has reply here:
what causing 'ca2202: not dispose objects multiple times' in code , how can refactor? 2 answersi'm using 2 objects follows:
using (var ms = new memorystream()) using (var bw = new binarywriter(ms)) { // work ms , bw, both referenced here }
it works "fine" , in fact an reply here too. however, when run vs2012's code analysis tool, warning like:
ca2202 not dispose objects multiple times object 'ms' can disposed more 1 time in method '<my method name>'. avoid generating system.objectdisposedexception should not phone call dispose more 1 time on object.
this leads me believe there might alternative way handle situation, don't know is.
does know 'proper' way utilize 2 objects within single using
block in warning-free manner?
the binarywriter class written such dispose underlying stream pass when dispose binarywriter unless utilize optional constructor parameter tell not to. since binary author disposing underlying memory stream , disposing of in using
block beingness disposed of twice.
now, isn't issue classes, should (and memory stream is) written work fine if disposed twice, long don't utilize after it's dispose (you're not) can safely ignore warning. if want warning go away can take memory stream out of using since it's beingness disposed.
c# dispose idisposable using using-statement
No comments:
Post a Comment