And what if I don't call dispose?
Will garbace collector "clean it up" after function is ended?
Not necessarily. The GC collects only those objects whose reference
count equals 0, that is, those objects that are no longer referenced by
any other object.
However, consider the case where object A references object B, and
object B references object A. When you're done using both, the GC will
query them to know whether it should delete those objects. However,
object A will have a reference to object B, so object B will not be
deleted. And object B has also a reference to object A, thus object A
never gets deleted. Therefore, neither object A nor B gets deleted and
you have a memory leak.
That's why the disposable-pattern was designed for. When you call
Dispose, apart from freeing non-managed resources, you're supposed to
set to null other references, in cases when objects double-refernce
themselves. In our example above, object B must set its reference to
object A, to null; and vice versa. So the GC can do its job.
Regards.