It is important to know the difference between an object and a variable:
- You want the caller variable to be updated to refer to a new object in some cases, so you need to go through the reference semantics. That means you need
ref or out - You need to read the existing value of the variable to find out whether to create a new object or not. That means you need
ref , not out . If you change it to the out parameter, your if will not compile because user will not be specifically assigned at the beginning of the method.
Personally, I'm not sure if this is a nice design. Are you sure that the method of creating a new object makes sense? Can you do it on the site? It feels a little awkward as it is.
Another alternative to using ref (but potentially creating a new user in this method) would be to return the corresponding link:
user = SetUserProperties(user); ... public User SetUserProperties(User user) { if(user == null) { user = new User(); } user.Firstname = "blah"; .... return user; }
source share