Checking if null before initializing is verbose. Use ??= operator for clean lazy initialization.
Old Verbose Way:
private List_cache; public List GetCache() { if (_cache == null) { _cache = new List (); } return _cache; }
Clean Way:
private List_cache; public List GetCache() { _cache ??= new List (); return _cache; } // If _cache is null, assign new List // If _cache already has value, do nothing
More Examples:
// Initialize config on first access _config ??= LoadConfiguration(); // Set default if null username ??= "Guest"; // Dictionary key initialization dict[key] ??= new List();
