Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Granted <code>IEnumerable</code> is an interface used in collections but the underlying implementation is an <strong>object</strong> and you have set that <code>collection</code> variable to point to <code>null</code>, hence the exception.</p> <p>If on the other hand you initialized <code>collection</code> with:</p> <pre><code>IEnumerable&lt;string&gt; collection = Enumerable.Empty&lt;string&gt;(); </code></pre> <p>OR</p> <pre><code>IEnumerable&lt;string&gt; collection = new List&lt;string&gt;(); </code></pre> <p>You would have an empty list <strong>object</strong> that you could act on. The <code>ArgumentNullException</code> exception is thrown because the collection <strong>argument</strong> is in fact <code>null</code> and that is what <code>ToArray()</code> is trying to act on. So logically, to me anyways, this was the only design choice.</p> <p><strong>Edit</strong></p> <p>On the other hand, in practice, I've made the conscious decision to always return a valid <code>IEnumerable&lt;T&gt;</code> when the return type on my class methods are supposed to return an <code>IEnumerable&lt;T&gt;</code>.</p> <p>For example; a method signature that looks like <code>IEnumerable&lt;T&gt; GetAll()</code> would always return a valid enumerable and If there was nothing to return then I would return <code>return Enumerable.Empty&lt;T&gt;();</code></p> <p>The difference here to me is that <code>GetAll()</code> is not acting on a collection <strong>argument</strong>. You could really look at this like the collection is really nothing more than a parameter to the method and if you passed in a null parameter to a regular method you would probably throw an <code>ArgumentNullException</code>.</p> <hr> <p><strong>The short answer</strong> is that your <code>collection</code> variable is an <em>expected</em> <strong>argument</strong> (or parameter) to the <code>ToArray()</code> method and it is <code>null</code> so it makes sense to throw an <code>ArgumentNullException</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload