DEV Community

Discussion on: No more NullReferenceException for Lists in C#

Collapse
 
mcurland profile image
Matthew Curland

Better yet, skip the assign when it isn't needed:
(list ?? (list = new List())).Add ("test");

Now, if the language allowed 'ref this' on the extension argument it could be totally transparent to the caller. Or be able to declare a create-if-null semantic on the variable if a default ctor is available (pretty sure VB4 did this 20+ years ago).