C
C#3y 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);
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?
2 Replies
Anton
Anton3y ago
User? means it can be null, which it can in your case. User means it can't be null
Accord
Accord3y ago
Closed!

Did you find this page helpful?