Skip to content

Instantly share code, notes, and snippets.

@testfirstcoder
Created February 24, 2016 09:09
Show Gist options
  • Save testfirstcoder/c7cd703166e8c965992f to your computer and use it in GitHub Desktop.
Save testfirstcoder/c7cd703166e8c965992f to your computer and use it in GitHub Desktop.
Functional using
public static class FunctionalUse
{
public static T Use<TDisposable, T>(Func<TDisposable> disposable, Func<TDisposable, T> expression)
where TDisposable : IDisposable
{
using (var client = disposable())
return expression(client);
}
public static void Use<TDisposable>(Func<TDisposable> disposable, Action<TDisposable> action)
where TDisposable : IDisposable
=> Use(disposable, _ => Do(() => action(_)));
private static Option Do(Action action)
{
action();
return Option.Nothing;
}
private enum Option
{
Nothing
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment