SeanJ
SeanJ
CC#
Created by SeanJ on 12/6/2022 in #help
✅ 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);
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?
4 replies