Let's say I create a disposable object in a method, but I will call a using out of the method. Is then everything disposed in this method?
using(DbConnection connection = new DbConnection("myConnection"){
SomeMethod();
}
public void SomeMethod(){
var stream = new MemoryStream()
// ... Do something with the stream ...
}
Is the stream created in the 'SomeMethod' then disposed?
No, it won't be. Only the reference explicitly specified in the using
statement will be disposed.
In this case, only connection
will be disposed after the code execution leaves the using block. You'll need to also wrap var stream = ..
in a using block, or manually dispose it.