Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, it will throw an <code>ArgumentNullException</code>. I can think of two reasons for putting the extra checking in:</p> <ul> <li>If you later go back and change the method to do something before calling <code>subjects.Count()</code> and forget to put the check in at that point, you could end up with a side effect before the exception is thrown, which isn't nice.</li> <li>Currently, the stack trace will show <code>subjects.Count()</code> at the top, and probably with a message with the <code>source</code> parameter name. This could be confusing to the caller of <code>HasFive</code> who can see a <code>subjects</code> parameter name.</li> </ul> <p>EDIT: Just to save me having to write it yet again elsewhere:</p> <p>The call to <code>subjects.Count()</code> will throw an <code>ArgumentNullException</code>, <em>not</em> a <code>NullReferenceException</code>. <code>Count()</code> is another extension method here, and assuming the implementation in <code>System.Linq.Enumerable</code> is being used, that's documented (correctly) to throw an <code>ArgumentNullException</code>. Try it if you don't believe me.</p> <p>EDIT: Making this easier...</p> <p>If you do a lot of checks like this you may want to make it simpler to do so. I like the following extension method:</p> <pre><code>internal static void ThrowIfNull&lt;T&gt;(this T argument, string name) where T : class { if (argument == null) { throw new ArgumentNullException(name); } } </code></pre> <p>The example method in the question can then become:</p> <pre><code>public static bool HasFive&lt;T&gt;(this IEnumerable&lt;T&gt; subjects) { subjects.ThrowIfNull("subjects"); return subjects.Count() == 5; } </code></pre> <p>Another alternative would be to write a version which checked the value <em>and returned it</em> like this:</p> <pre><code>internal static T NullGuard&lt;T&gt;(this T argument, string name) where T : class { if (argument == null) { throw new ArgumentNullException(name); } return argument; } </code></pre> <p>You can then call it fluently:</p> <pre><code>public static bool HasFive&lt;T&gt;(this IEnumerable&lt;T&gt; subjects) { return subjects.NullGuard("subjects").Count() == 5; } </code></pre> <p>This is also helpful for copying parameters in constructors etc:</p> <pre><code>public Person(string name, int age) { this.name = name.NullGuard("name"); this.age = age; } </code></pre> <p>(You might want an overload without the argument name for places where it's not important.)</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. 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.
 

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