C
C#15mo 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
Angius15mo ago
So you return a Task<T>? ? Why? Why not Task<T?>?
r2d25551
r2d25551OP15mo 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
Angius15mo ago
uh awaiting strips the Task away
r2d25551
r2d25551OP15mo ago
The return type is derived from EventArgs
Angius
Angius15mo ago
args will be of type MyEventArgs? Not Task<MyEventArgs?>
r2d25551
r2d25551OP15mo ago
Trying now. . .
Omnissiah
Omnissiah15mo ago
or use var
Angius
Angius15mo ago
That's the easy way out, yeah lol
r2d25551
r2d25551OP15mo ago
Thank you 😄 😄 😄 that worked. I need to not that await strips the task out.
Accord
Accord15mo 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.
Want results from more Discord servers?
Add your server