What do you get when you marry the IDisposable interface to the Action delegate?
Disposable Actions... with a bit of syntactic sugar.
As you may or may not know, you can use the using block on types that implement the IDisposable interface.
When the block is exiting it will call dispose on the type.
For example the following block of code uses the "using" block.
But when compiled, the above block of code actually expands out to something like…
Ok, so now let’s take a look at the signature for the Action delegate.
The signature returns void and takes in an object of type T. Quite simple actually. I’m sure you can think of quite a few methods that match that signature, but just in case you can’t here are a couple of examples.
Ok, hopefully that makes sense! Now on to the good stuff.
Suppose we created a type that implemented the IDisposable interface, and took in our closing action.
This action would run when a process is completed.
In this scenario the “Dispose” method is not being used to clean up resources, but rather to signal the end of a running process.
For instance suppose we wanted to write a message to the console upon completion of a long running process.
The above code uses the “using” block to wrap a sequence of events to call an action when the sequence of events is completed.
When the “do something” block of code is completed the “Finished running…” message will be spit out to the console.
The underlying implementation looks like this…
Perhaps the following block of code might make a little more sense to you now…