C
C#2y ago
r2d25551

❔ await nullable return values

I have a function returning a nullable class. The compiler complains at the await line it cannot implicity perform the conversion even though they are both the same type.
10 Replies
Angius
Angius2y ago
So you return a Task<T>? ? Why? Why not Task<T?>?
r2d25551
r2d25551OP2y ago
public class MyEventArgs : EventArgs
{
public string data { get; set; } = string.Empty;
}


protected async Task<MyEventArgs?> MyTask()
{
bool done = true;

if (done)
{
return (MyEventArgs?)null;
}

return new MyEventArgs{ data = "" };
}

protected aysnc MoreToProcess()
{
Task<MyEventArgs?> args = await MyTask();
}
public class MyEventArgs : EventArgs
{
public string data { get; set; } = string.Empty;
}


protected async Task<MyEventArgs?> MyTask()
{
bool done = true;

if (done)
{
return (MyEventArgs?)null;
}

return new MyEventArgs{ data = "" };
}

protected aysnc MoreToProcess()
{
Task<MyEventArgs?> args = await MyTask();
}
Angius
Angius2y ago
uh awaiting strips the Task away
r2d25551
r2d25551OP2y ago
The return type is derived from EventArgs
Angius
Angius2y ago
args will be of type MyEventArgs? Not Task<MyEventArgs?>
r2d25551
r2d25551OP2y ago
Trying now. . .
Ꜳåąɐȁặⱥᴀᴬ
or use var
Angius
Angius2y ago
That's the easy way out, yeah lol
r2d25551
r2d25551OP2y ago
Thank you 😄 😄 😄 that worked. I need to not that await strips the task out.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?