Question

When daisy chaining multiple blocks that modify the same TInput elements, I can either use ActionBlock<TInput> or TransformBlock<TInput,TInput>, the difference being that the Transform's delegate would return the same (but modified element) while the Action delegate wouldn't return anything, it would just modify the input element.

Both work, and I suppose both make some sense semantically, but I'm assuming only one of those is the intended way.

If I had to guess, I'd say it's Action because when returning the same elements, it's not really a transformation. But if I had to guess for more pragmatic reasons, I'd say Transform, because if I'm daisy chaining these blocks Transform seems to allow me to post a "processed" element straight to the next block, not sure how I could enforce that with Action.

Was it helpful?

Solution

If you want to modify the object in your block and then work with it in another block, then the only correct option is to use a TransformBlock. I'd say that it is a transformation: the resulting object has the same identity as the original, but it was modified (or “transformed”) in the block.

ActionBlock is most commonly used as the end of a pipeline, its final step. That's not what you want here, so ActionBlock is not appropriate.

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