title: C# Generic List Property Set/Add behaviour
body: <p>I want to store a generic list in the viewstate in an ASP.NET user control, so I have the following code:</p>
  
  <pre><code>    protected List&lt;MoodleCourse&gt; MoodleCoursesCreated
      {
          get
          {
              if (ViewState["MoodleCoursesCreated"] == null)
              {
                  return new List&lt;MoodleCourse&gt;();
              }
              else
              {
                  return (List&lt;MoodleCourse&gt;)ViewState["MoodleCoursesCreated"];
              }
          }
          set
          {
              ViewState["MoodleCoursesCreated"] = value;
          }
      }
  </code></pre>
  
  <p>So to add an item to the list, I call <code>MoodleCoursesCreated.Add(new MoodleCourse);</code></p>
  
  <p>However it seems then I must do <code>MoodleCourseCreated = MoodleCourseCreated</code> to activate the setter so the list actually gets stored in the viewstate. I suspect there is a more elegant way to achieve this, does anyone have any suggestions? Cheers</p>