Possible compiler bug - no warning when calling extension method w/o await #6176
-
#nullable enable
using System;
using System.Threading.Tasks;
public class C {
ActionAsync? _action;
public async Task M() {
await Task.Delay(100);
this._action?.InvokeAsync(); // no warning here (this caused a bug for me)
if(this._action != null)
this._action(); // however, this one does get a warning (as expected)
}
}
public delegate Task ActionAsync();
public static partial class _Delegate_Extensions
{
public static async Task InvokeAsync(this ActionAsync? This)
{
if (This != null)
{
foreach (ActionAsync d in This.GetInvocationList())
{
await d();
}
}
}
} VS does offer to add an await on that PS. Never mind - I figured it out: I mistakenly added a |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Looks like it's the result of the null-propagation operator, removing that will emit the warning. But an |
Beta Was this translation helpful? Give feedback.
Looks like it's the result of the null-propagation operator, removing that will emit the warning. But an
await
there would throw if_action
isnull
, so it's not exactly a good pattern in general, at least withoutawait?
or the like.