Search code examples
c#asynchronouslambdadelegatesblazor

c# blazor avoid using 'async' lambda when delegate type returns 'void'


I get the following warning in JetBrains Rider and I can't find a way to workaround it. It's a blazor WASM project with .net 6.

Avoid using 'async' lambda when delegate type returns 'void'

Sample code Razor:

<Validation Validator="async e => await ValidateFieldAsync(e)">

Sample code c#:

protected async Task ValidateFieldAsync(ValidatorEventArgs args)
{
    // Some code with awaits etc.
}

Jetbrains describes this warning here:

https://www.jetbrains.com/help/resharper/AsyncVoidLambda.html

But what is the best practice here to fix this?

Thanks for any hints!


Solution

  • It looks like Resharper lost track here. The warning is incorrect.

    But you can improve this anyway,

    <Validation Validator="e => ValidateFieldAsync(e)">
    

    As long as ValidateFieldAsync() still returns a Task this is still async and awaitable, just with a little less overhead. The aync and await in the lambda were adding an extra layer that isn't needed. And it might just stop that false warning, I can't check now.

    When you don't need any argument or when Blazor can auto add it then you can follow @MisterMagoo's answer.