A simple library to bring GoLang's defer
functionality to .NET!
DotNetDefer
- Implements
IDisposable
andIAsyncDisposable
, and can be used withAction
,Func<T>
Func<Task>
andFunc<Task<T>>
- Implements
DotNetDefer.Legacy
- Implements only
IDisposable
and can be used withAction
s andFunc
s
- 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.DotNetDefer
There's also a version of the package that targets netstandard1.0
for compatibility with older projects. (E.g. projects that don't support IAsyncDisposable
)
Install-Package DrEsteban.DotNetDefer.Legacy