TransformManyBlock, will returning empty IEnumerables need to be explicitly handled in order to not have them retained in memory?

StackOverflow https://stackoverflow.com/questions/14891023

Question

I implemented a TransformManyBlock<Tin,Tout> and I wonder when I return an empty IEnumerable within the block will that empty IEnumerable be retained anywhere in the system or will it be garbage collected or discarded for me? Obviously I would need to handle such IEnumerable myself if I instead used a TransformBlock but I wonder whether this is already handled by the library for TransformManyBlock.

I guess what I like to know is whether TPL will handle the disposing of that empty IEnumerable for me or whether I need to handle it myself.

Was it helpful?

Solution

IEnumerable<T> doesn't implement IDisposable and most - if not all - standard implementations of IEnumerable<T> don't implement IDisposable either. So the TPL doesn't dispose it for you if you actually return an instance of a collection that needs disposing.
But this really should never be the case if you want to return an empty enumerable, because in that case you could just return Enumerable.Empty<T>() or new T[0].

Actually, I think you are confusing "disposing" and "garbage collection".
The empty enumerable will be available for garbage collection as soon as there are no more references pointing to it.

So, to sum it up: You don't have to worry about that, except when you want to return a very exotic enumerable implementation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top