Skip to content

Instantly share code, notes, and snippets.

@jfoshee
Created August 20, 2022 19:40
Show Gist options
  • Save jfoshee/04af9707b613a487b5159f3c722007dc to your computer and use it in GitHub Desktop.
Save jfoshee/04af9707b613a487b5159f3c722007dc to your computer and use it in GitHub Desktop.
Convenient wrapper for an Action that must be called in spite of exceptions. Use with a `using` statement.
using System;
/// <summary>
/// Convenient wrapper for an Action that must be called in spite of exceptions.
/// Use with a `using` statement.
/// <code>
/// using var _ = new ActionDisposable(action);
/// </code>
/// </summary>
public class ActionDisposable : IDisposable
{
private readonly Action disposeAction;
public ActionDisposable(Action disposeAction)
{
this.disposeAction = disposeAction ?? throw new ArgumentNullException(nameof(disposeAction));
}
public void Dispose() => disposeAction();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment