System.Lazy to Lazy Initialization をエンティティで使用したい:
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 にあります。次のエラーが発生します: The property ' 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 } }
私は何か間違ったことをしていますか?