In c# 9 with the new feature of Nullable reference types i find myself putting "?" on every class properties types to turn of the warning and then placing if != null outside in the code to avoid null references (and that's good). But i was wondering, is it right to spam "?" everywhere?
Let's say i have a class that represents a database record with a foreign key that is not optional:
public int UserId { get; set; }
public virtual User User { get; set; }
I expect the virtual property to be always not null (since it is instantiated by entity framework) so i don't want to make it nullable to remove the warning in the class, instead i would initialize it's value to "null!". I should then wrap the code that uses this property with a try catch block to catch an exception if entity framework won't work as expected.
What do you think? Is it safer to spam "?" and always repeat if != null everywhere?