A simple library to bring GoLang's defer functionality to .NET!
DotNetDefer- Implements
IDisposableandIAsyncDisposable, and can be used withAction,Func<T>Func<Task>andFunc<Task<T>>
- Implements
DotNetDefer.Legacy- Implements only
IDisposableand can be used withActions andFuncs
- Implements only
using DotNetDefer;
class Program
{
static void Main(string[] args)
{
var process = new MyLongProcess();
process.Start();
using var defer = new Defer(() => process.Stop());
// Do some work
}
}dotnet add package DrEsteban.DotNetDefer
Install-Package DrEsteban.DotNetDeferThere's also a version of the package that targets netstandard2.0 for compatibility with older projects. (E.g. projects that don't support IAsyncDisposable)
Install-Package DrEsteban.DotNetDefer.Legacy