Should I use the ref or out keyword here?

Posted by Blankman on Stack Overflow See other posts from Stack Overflow or by Blankman
Published on 2010-03-31T18:39:31Z Indexed on 2010/03/31 18:43 UTC
Read the original article Hit count: 295

Filed under:
|

I have an object that may be null, which I will pass to a method that will set its properties.

So my code looks like:

User user = null;  // may or may not be null at this point.

SetUserProperties(user);

UpdateUser(user);


public void SetUserProperties(User user)
{
      if(user == null) 
          user = new User();

     user.Firstname = "blah";
     ....

}

So I am updating the same object I pass into SetUserProperties.

Should I use the 'ref' keyword in my method SetUserProperties?

© Stack Overflow or respective owner

Related posts about c#

Related posts about pass-by-reference