I use ?? operator very heavily in my code. But today I just came across a question.
Here the code which I use for ?? operator
private List<string> _names;
public List<string> Names
{
get { return _names ?? (_names = new List<string>()); }
}
But in some locations I have seen this code as well.
private List<string> _names;
public List<string> Names
{
get { return _names ?? new List<string>(); }
}
What is real difference between these codes. In one I am assigning _names = new List() while in other I am just doing new List().
in second case you not assign
_names
withnew List<string>()
, if_names
null, every time when you callNames
it will createnew List<string>()
, but in first casenew List<string>()
create ones.