我想在我的实体中使用 System.Lazy 来延迟初始化我的列表:
public class Questionary
{
private Lazy<List<Question>> _questions = new Lazy<List<Question>>(() => new List<Question>());
public IList<Question> Questions { get { return _questions.Value; } set { _questions.Value = value; } }
}
问题出在我的 SETTER 上,出现此错误:属性 ' System.Lazy<T>.Value
' has no setter
如果我想做什么MyInstance.Questions = new List<Question> { ... }
?
我该如何进行?
更新:
我试图避免这种情况:
private IList<Question> _questions;
//Trying to avoid that ugly if in my getter:
public IList<Question> Questions { get { return _questions == null ? new List<Question>() : _questions; } set { _questions = value } }
我做错了什么?