Cached Property: Easier way?

前端 未结 7 2069
傲寒
傲寒 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:40

    As far as syntax goes, you can use the null-coalescing operator if you want to be fancy, but it's not necessarily as readable.

    get
    {
        return notes ?? (notes = CalcNotes());
    }
    

    Edit: Updated courtesy of Matthew. Also, I think the other answers are more helpful to the question asker!

提交回复
热议问题