Cached Property: Easier way?

前端 未结 7 2070
傲寒
傲寒 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? notes;
    public List Notes => notes ??= CalcNotes();
    

提交回复
热议问题