Search code examples
.netfinalizerbufferedstream

How does Stream.Dispose(bool) get called by the Finalize method?


The Stream.Dispose(bool) docs say:

This method is called by the public Dispose method and the Finalize method. [..] Finalize invokes Dispose with disposing set to false.

However, neither Stream nor its super class MarshalByRefObject have a destructor, and the destructor of Object is empty.

So how can Stream.Dispose(bool) get called by the Finalize method?


Solution

  • Stream is a abstract class so you are guaranteed that at least one layer lower should be inherited. In that inherited class is where the finalizer exists.

    I don't know their exact reasons, but one potentially could be is if you don't need the finializer it is not forced on you due to the non zero overhead of having one, the biggest cost being your object likely wont be collected during the Gen 0 Garbage collection even if it is very short lived (see this linked article for details).