0

Can I replace following function

public async Task<User> GetUserAsync(int id)
{
   return await _dbContext.Users.SingleAsync(id);
}

with:

public Task<User> GetUserAsync(int id)
{
   return _dbContext.Users.SingleAsync(id);
}
Vidhya Sagar Reddy
  • 1,521
  • 1
  • 13
  • 25

1 Answers1

0

I'm going to make a plausible assumption here: _dbContext.Users.SingleAsync(id); returns a Task<User>.

It's bad style to use async when you can just call a single function that returns a Task of the right type. It will handle the async for you. Your code size will be smaller, and your generated code size will be much smaller.

Controversial opinion: If there's a single async in the middle it should be replaced with Task.ContinueWith().

Joshua
  • 40,822
  • 8
  • 72
  • 132