Say I have this:
private list<myClass> myCollection;
Is there a programming idiom to shorten this very common statement combination into a single statement? :
if (myCollection == null)
myCollection = new list<myCollection>();
myCollection.Add(someInstanceOfMyClass);
The idiom could be described as “create if necessary, then add to it”
TIA.
Why not simply create at declaration?:
Instantiating a single list is not a time or memory consuming thing. (Might be different if you had thousands of these and wanted lazy instantiation).