Cached Property: Easier way?

前端 未结 7 2012
傲寒
傲寒 2021-01-01 10:00

I have a object with properties that are expensive to compute, so they are only calculated on first access and then cached.

 private List notes;
         


        
相关标签:
7条回答
  • 2021-01-01 10:49

    A two-line option with C# 8 and null-coalescing assignment:

    private List<Note>? notes;
    public List<Note> Notes => notes ??= CalcNotes();
    
    0 讨论(0)
提交回复
热议问题