Avoiding duplicate methods for Task and Task<T>
.net, c#, task
Solution
You can make the `Task` overload call the other one, and return a dummy value.
public async Task WrapException(Func<Task> task)
{
await WrapException<object>(async () => {
await task();
return null;
});
}
Or, since the `async` keyword is unnecessary here:
public Task WrapException(Func<Task> task)
{
return WrapException<object>(async () => {
await task();
return null;
});
}
Problem
I have some logic for `Task` and `Task<T>`. Is there any way to avoid duplicating code ? My current code is following: ``` public async Task<SocialNetworkUserInfo> GetMe() { return await WrapException(() => new SocialNetworkUserInfo()); } public async Task AuthenticateAsync() { await WrapException(() => _facebook.Authenticate()); } public async Task<T> WrapException<T>(Func<Task<T>> task) { try { return await task(); } catch (FacebookNoInternetException ex) { throw new NoResponseException(ex.Message, ex, true); } catch (FacebookException ex) { throw new SocialNetworkException("Social network call failed", ex); } } public async Task WrapException(Func<Task> task) { try { await task(); } catch (FacebookNoInternetException ex) { throw new NoResponseException(ex.Message, ex, true); } catch (FacebookException ex) { throw new SocialNetworkException("Social network call failed", ex); } } ```