My current code seems like this:
// for given methods like these:
// void Foo(Action<int> action)
// async Task DoAsync()
Foo(unusedInt =>
{
var unusedTask = DoAsync();
});
I know I can use the discard variable(_
) since C#7.0, like this:
Foo(_ =>
{
var unusedTask = DoAsync();
});
Or,
Foo(unusedInt =>
{
_ = DoAsync();
});
But I meet an error if I use _
for both of them:
Foo(_ =>
{
_ = DoAsync(); // error CS0029
});
error CS0029: Cannot implicitly convert type 'System.Threading.Tasks.Task' to 'int'
Is there anyway to discard both of the unused variables?
Or, can anyone confirm that it would not be possible within the current C# spec?
For reference,
If I omit the unusedTask
:
Foo(_ =>
{
DoAsync(); // warning CS4014
});
warning CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
I'd like to avoid this warning, either.