Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the C# compiler automatically dispose of IDisposable objects?

Assuming I have a method public static Rectangle DrawRectangle(Vector origin, Vector size) which returns an object of type Rectangle : IDisposable

If I call only the method DrawRectangle(origin, size), but do not assign the return value to a variable myRectangle = DrawRectangle(origin, size), will the compiler automatically detect this and call DrawRectangle(origin, size).Dispose(), or do I have to do it myself?

like image 642
Nolonar Avatar asked Dec 03 '25 17:12

Nolonar


1 Answers

No. Point.

Eventually he finalizer does, but no. It does not automatically CALL Dispose.

like image 152
TomTom Avatar answered Dec 06 '25 07:12

TomTom