title: C# generics list of objects used as a property - can't add values
body: <p>I'm trying generics for the first time and am having a problem.</p>
  
  <p>I have a dll that sends messages in batches</p>
  
  <ul>
  <li><p>there is a "Message" class and a "Batch" class in that dll</p></li>
  <li><p>on the batch class, I have some public properties</p></li>
  <li><p>on of the batch class's public properties is a property called "Messages" which is a list of the "Message" class as follows:</p>
  
  <pre><code> public List&lt;Message&gt; Messages {get;set;}
  </code></pre></li>
  </ul>
  
  <p><strong>Method 1</strong></p>
  
  <p>I then have a test exe where I want to set the properties on the "Batch" class as follows:</p>
  
  <pre><code>Batch myBatch = new Batch()
  myBatch.Messages.Add(
    new MyNameSpace.Message(txtToAddress.Text, txtMessage.Text));
  </code></pre>
  
  <p>When I run the app, I get:</p>
  
  <p>"Object reference not set to an instance of an object."</p>
  
  <p><strong>Method 2</strong></p>
  
  <p>After playing around a bit, I see that I can successfully do the following in the test exe:</p>
  
  <pre><code>List&lt;MyNameSpace.Message&gt; myMessages = new List&lt;MyNameSpace.Message&gt;();
  myBatch.Messages.Add(
   new MyNameSpace.Message(txtToAddress.Text, txtMessage.Text));
  myBatch.Messages = myMessages;
  </code></pre>
  
  <p>I'd like to get it working in the first way because other programmers will be using the dll and it seems more intutive to use the first approach.</p>
  
  <p>What am I missing to get the first method to work?</p>