title: C# initialize list so not null if no items added
body: <p>I have a routine which initializes a list ...</p>
  
  <pre><code>var items = new List&lt;TodoItem&gt;();
  //... load items from a REST service ...
  return items;
  </code></pre>
  
  <p>A subsequent routine tries to enumerate the list ...</p>
  
  <pre><code>foreach (var i in items)
  {
      // ...
  }
  </code></pre>
  
  <p>If the portal doesn't return any items, the list is <code>null</code> and the attempt to enumerate it fails ... the reasons have already been discussed.</p>
  
  <p>Till now I did a <code>!= null</code> check before the enumeration. Now I read a SO post where someone suggest that it was "bad practice" to return <code>null</code> in case a list has no items.</p>
  
  <p>How do I initialize the list as an empty list which can be enumerated, instead of letting it be <code>null</code>? the only way that came to my mind was a sequence like</p>
  
  <pre><code>var items = new List&lt;TodoItem&gt;();
  items =  Enumerable.Empty&lt;TodoItem&gt;()
  </code></pre>
  
  <p>This won't compile:</p>
  
  <blockquote>
    <p>Cannot implicitly convert type System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?).</p>
  </blockquote>
  
  <p>Trying to use an explicit cast ...</p>
  
  <pre><code>items = (List&lt;TodoItem&gt;)Enumerable.Empty&lt;TodoItem&gt;();
  </code></pre>
  
  <p>fails with a runtime exception "invalid cast"</p>
  
  <p>Can anybode advise how I can properly initialize my list?</p>