That code is essentially the same as this:
private Person _user;
private Person User
{
get
{
if (_user == null) _user = GetUser();
return _user;
}
}
How it works
The null coalescing operator (??
) returns the object if it's not null, otherwise it returns whatever is on the other side of the operator. So the statement:
return _user ?? ( _user = GetUser() );
Says, "return _user
, unless it's null, in which case return the result of the assignment ( _user = GetUser() )
". This is a clever way of assigning a value to _user
and returning that assigned value in the same line.
That being said, some developers will argue that the first method I wrote above, which uses two lines instead of one, is more readable (the intent is clearer) and easier to maintain.