C#C
C#4y ago
SeanJ

✅ Best Practice on Return Types for Services in WebAPI

Looking for a more of an insight into the better practice of return types with nullability. Lets say I have the following code
 public async Task<User> GetById(int userId) => 
        await _dbContext.Users.FirstOrDefaultAsync(x => x.Id == userId); 


Should this be Task<User?> GetById or Task<User> for best practice and why?
Was this page helpful?