Prefer Func<Task> over Func<ValueTask> overload when using async lambdas #4360
-
I have one method with two overloads: void Foo(Func<Task> task)
void Foo(Func<ValueTask> task) All works well until async lambda is passed as a first argument: Can I somehow set |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I did it by using a dummy optional parameter: So in your case that would be: /// <summary>
/// A class with no possible value other than null. Used to mark an optional parameter which should never be set.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class DummyParameter
{
private DummyParameter() { }
}
void Foo(Func<Task> task)
void Foo(Func<ValueTask> task, DummyParameter? _ = null) |
Beta Was this translation helpful? Give feedback.
-
Another idea: use different names and named argument? void Foo(Func<Task> taskFunc);
void Foo(Func<ValueTask> valueTaskFunc);
Foo(taskFunc: () => ...) (haven't tried it, but I think it could work) |
Beta Was this translation helpful? Give feedback.
I did it by using a dummy optional parameter:
https://github.com/YairHalberstadt/stronginject/blob/86f0e766bb2a2028d302f9402db166ca42cb7b84/StrongInject/ContainerExtensions.cs#L44-L56
So in your case that would be: