Returning 0 from a generic async method triggers NullGuard
See original GitHub issueHere’s a simplified version of the situation where I noticed this happening:
public async Task<T> GetThing(Func<T> getThing)
{
return getThing();
}
GetThing(() => 0);
I’m guessing this happens because you can’t compare the generic return value to null
, since it may not be an object. So the generated code is probably comparing to default(T)
, but for struct types default(T)
is not null, and is typically a perfectly legitimate value.
Issue Analytics
- State:
- Created 6 years ago
- Comments:11 (10 by maintainers)
Top Results From Across the Web
Tell the caller that a `async Task<T>` method may return null
My situation is similar to the one described Proper nullable annotation for async generic method that may return default(T).
Read more >Cannot Have Nullable Return of Async Generic Method
However, I'm running into a problem with a Generic Async method. [return: MaybeNull] public async Task<T> GetAsync<T>() => default;.
Read more >Returning null from Task-returning methods in C#
In this blog post, we'll understand this behavior and the difference between returning null from an async vs. non- async method in C#....
Read more >Async return types (C#)
Learn about the return types that async methods can have in C# with code examples for each type.
Read more >Help with async await
When the await is done awaiting, it automatically uses Unity SynchronizationContext and continues from where it left off some frames later, at ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@tom-englert You, sir, are a hero.
I’ll take care of this