Created
February 24, 2016 09:09
-
-
Save testfirstcoder/c7cd703166e8c965992f to your computer and use it in GitHub Desktop.
Functional using
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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