如果初始化可能会抛出异常,我应该避免使用 Lazy<T> class 吗?
Should I avoid using the Lazy<T> class if initialization might throw an exception?
鉴于我 the Lazy<T>
in a public property. Should I avoid such a design if lazy-initialization might throw an exogenous exception?
访问Value
属性时会抛出异常。和 property getters should not throw exceptions。另一方面,Lazy<T>
在将初始化异常传递给 Value
属性 getter.
的领域中经过精心设计和详细记录
在这种情况下最好公开一个方法,让懒惰成为一个实现细节。
不要使用惰性初始化来实现 public 属性,以免引发异常,原因与 属性 设计指南中给出的原因相同。
鉴于我Lazy<T>
in a public property. Should I avoid such a design if lazy-initialization might throw an exogenous exception?
访问Value
属性时会抛出异常。和 property getters should not throw exceptions。另一方面,Lazy<T>
在将初始化异常传递给 Value
属性 getter.
在这种情况下最好公开一个方法,让懒惰成为一个实现细节。
不要使用惰性初始化来实现 public 属性,以免引发异常,原因与 属性 设计指南中给出的原因相同。