Is an object suitable for garbage collection if it is created as part of a use-instruction and is clearly not bound to a link?

I have this (for illustration only) C # code:

using( new System.IO.MemoryStream() ) { System.Threading.Thread.Sleep(1000); } 

Note that a MemoryStream is created here and is clearly not bound to a link. Therefore, if there is no special treatment due to the using statement, the object has no references to it and can be assembled before the control leaves the using statement and, possibly, even before Sleep() .

Is a MemoryStream suitable for collection before control leaves the using statement?

+4
source share
2 answers

No that's not.

A hidden link to the MemoryStream was created behind the scenes, so it’s still alive.

+3
source

No.

The using statement compiles into a finally block that provides the object.

Thus, it is still in scope until the end of the block.

+8
source

All Articles