entity framework - Why create a method within a model that creates a list object -


i found example in question. wonder wat purpose served method question(). seems when question object created answer property created list object of answer[s].

this first time have seen technique, new programmer, benefit pattern?

public class question {    public question()    {       this.answers = new list<answer>();    }    public int questionid { get; set; }    public string title { get; set; }    public virtual icollection<answer> answers { get; set; }  }  public class answer {    public int answerid { get; set; }    public string text { get; set; } } 

i find pattern useful make consumption of object easier. i.e., creating answers list in constructor, ensured answers never null. makes easier work question object. so, in code consumes question object, can this

foreach (answer in question.answers) {     ... } 

without having first check if questions.answers null:

if (question.answers != null) {    foreach (answer in question.answers)    {        ...    } } 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -