C
C#10mo 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
Angius10mo ago
So you return a Task<T>? ? Why? Why not Task<T?>?
r2d25551
r2d2555110mo 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
Angius10mo ago
uh awaiting strips the Task away
r2d25551
r2d2555110mo ago
The return type is derived from EventArgs
Angius
Angius10mo ago
args will be of type MyEventArgs? Not Task<MyEventArgs?>
r2d25551
r2d2555110mo ago
Trying now. . .
WEIRD FLEX
WEIRD FLEX10mo ago
or use var
Angius
Angius10mo ago
That's the easy way out, yeah lol
r2d25551
r2d2555110mo ago
Thank you 😄 😄 😄 that worked. I need to not that await strips the task out.
Accord
Accord10mo 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.