Consider this example, it shows two possible ways of lazy initialization. Except for being thread-safe, are there any specific advantates of using Lazy<T> here?
class Customer {
private decimal? _balance2;
private static decimal GetBalanceOverNetwork() {
//lengthy network operations
Thread.Sleep(2000);
return 99.9M;
}
public decimal? GetBalance2Lazily() {
return _balance2 ?? (_balance2 = GetBalanceOverNetwork());
}
private readonly Lazy<decimal> _balance1 = new Lazy<decimal>(GetBalanceOverNetwork);
public Lazy<decimal> Balance1 {
get { return _balance1; }
}
}
最新资料:
见上文准则,将其作为一个简单的例子,数据类型不相干,这里的问题是比较拉齐尔;T>而不是标准 la的初始化。