gpt4 book ai didi

c# - 缓存属性 : Easier way?

转载 作者:IT王子 更新时间:2023-10-29 04:02:25 25 4
gpt4 key购买 nike

我有一个对象,其属性的计算成本很高,因此它们仅在第一次访问时计算然​​后缓存。

 private List<Note> notes;
public List<Note> Notes
{
get
{
if (this.notes == null)
{
this.notes = CalcNotes();
}
return this.notes;
}
}

我想知道,有没有更好的方法来做到这一点?是否有可能在 C# 中创建缓存属性或类似的东西?

最佳答案

就语法而言,您可以使用 null-coalescing运算符,如果你想花哨,但它不一定具有可读性。

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

编辑:由 Matthew 提供更新。另外,我觉得其他答案对提问者更有帮助!

关于c# - 缓存属性 : Easier way?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2579329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com