C
C#2y 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
Anton2y ago
User? means it can be null, which it can in your case. User means it can't be null
Accord
Accord2y ago
Closed!
Want results from more Discord servers?
Add your server