How to reassign a one-time object variable?

In C # when reassigning a one-time object variable with a new object, how does it work in memory? Will the memory space occupied by the old object be simply overwritten by the new object? Or do I still need to call Dispose()to free the resources that it uses?

DisposableThing thing;

thing = new DisposableThing();
//....do stuff
//thing.Dispose();
thing = new DisposableThing();
+5
source share
3 answers

It works just like any other purpose: =does not care about IDisposableor does not do any magic.

, , Dispose, (, , using). , Dispose : ?

, , ?

. "name" objects. , - , " ". (. : , #, .)

, * (, , [, ]). - , , Dispose .

.


, - ! - , , " ", . , using .

* , , , / , . , .

+3

/ IDisposable. . IDisposable.

using (thing = new DisposableThing()) {
  // ... do stuff
}

thing = new DisposableThing();

thing using

+5

IDisposable-. , , , , Dispose, .

, IDisposable.Dispose - , , - (, GDI, ..), , . , - IDisposable, , , , .

+1

All Articles