title: Does it make sense to allow IList parameter to be null or throw exception if null?
body: <p>Here's what I came accross yesterday. I'm wondering what is the best practise here. I can make an instance of this class like this or parameter can be null coming from other places:</p>
  
  <pre><code>CustomItemsSourceDialogViewModel itemsSource = new CustomItemsSourceDialogViewModel();
  itemsSource.Initialize(null); // I get nullReferenceException
  </code></pre>
  
  <p>Here's the mentioned class definition:</p>
  
  <pre><code>public class CustomItemsSourceDialogViewModel
  {
    public void Initialize(IList&lt;string&gt; items)
    {
      // it doesn't make sense to allow parameter items to be null
      // it only makes sense to allow not null items collection
      // so we can operate on it.
      // if items is null we get system.nullReferenceException
      if (items.Count &gt; 0)
        // aggregate is a system.linq static method
        this.ItemsSource = items.Aggregate((x, y) =&gt; x + "\r\n" + y);
    }
  }
  </code></pre>
  
  <p>I prefer to keep my collections / lists not null. In other words collections should have a default value of empty collections. This way developer or user won't get that nasty NullReferenceException.</p>
  
  <p>Regarding list parameter to a method, should I throw exception and notify the user when parameter is null:</p>
  
  <pre><code>public void Initialize(IList&lt;string&gt; items)
  {
    if( items == null )
      throw new Exception("Parameter items is null. It should be a valid list.");
    ...
  </code></pre>
  
  <p>The other solution would be to check if it's not null:</p>
  
  <pre><code>public void Initialize(IList&lt;string&gt; items)
  {
    if (items != null &amp;&amp; items.Count &gt; 0)
      this.ItemsSource = items.Aggregate((x, y) =&gt; x + "\r\n" + y);
   }
  </code></pre>
  
  <p>But checking always if not null seems a bit akward.</p>