Reputation: 11
I've a method like this:
public async Task<Response> HandleRequest(string connectionId, Request request)
{
if (request is AuthorizeRequest)
{
return await _handler.HandleRequest(connectionId, request as AuthorizeRequest);
}
if (request is ChangeConfigurationRequest)
{
return await _handler.HandleRequest(connectionId, request as ChangeConfigurationRequest);
}
return await Task.FromResult<Response>(null); //My question is here
}
My question is: Should I return return await Task.FromResult(null); or retun null; Because if request is not 'AuthorizeRequest' and 'ChangeConfigurationRequest'
Tks all for helps
Upvotes: 1
Views: 3318
Reputation: 456437
I search from many resources and it said that we should not return null in Task. Is it right?
Methods that return a task should never return a null
task. However, what you want to do is return a (non-null
) task that contains a result value of null
. That's fine; it's completely different than returning a null
task.
For your original code, use return null;
. Do not ever use await Task.FromResult(...)
.
Upvotes: 2
Reputation: 14534
Since your method is marked as async
, you should return null
.
Upvotes: 2