Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 172825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:14:03+00:00 2026-05-11T13:14:03+00:00

Lets say I have this extention method: public static bool HasFive<T>(this IEnumerable<T> subjects) {

  • 0

Lets say I have this extention method:

public static bool HasFive<T>(this IEnumerable<T> subjects) {     if(subjects == null)         throw new ArgumentNullException('subjects');      return subjects.Count() == 5; } 

Do you think this null check and exception throwing is really necessary? I mean, when I use the Count method, an ArgumentNullException will be thrown anyways, right?

I can maybe think of one reason why I should, but would just like to hear others view on this. And yes, my reason for asking is partly laziness (want to write as little as possible), but also because I kind of think a bunch of null checking and exception throwing kind of clutters up the methods which often end up being twice as long as they really needed to be. Someone should know better than to send null into a method :p

Anyways, what do you guys think?


Note: Count() is an extension method and will throw an ArgumentNullException, not a NullReferenceException. See Enumerable.Count<TSource> Method (IEnumerable<TSource>). Try it yourself if you don’t believe me =)


Note2: After the answers given here I have been persuaded to start checking more for null values. I am still lazy though, so I have started to use the Enforce class in Lokad Shared Libraries. Can recommend taking a look at it. Instead of my example I can do this instead:

public static bool HasFive<T>(this IEnumerable<T> subjects) {     Enforce.Argument(() => subjects);     return subjects.Count() == 5; } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T13:14:04+00:00Added an answer on May 11, 2026 at 1:14 pm

    Yes, it will throw an ArgumentNullException. I can think of two reasons for putting the extra checking in:

    • If you later go back and change the method to do something before calling subjects.Count() 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.
    • Currently, the stack trace will show subjects.Count() at the top, and probably with a message with the source parameter name. This could be confusing to the caller of HasFive who can see a subjects parameter name.

    EDIT: Just to save me having to write it yet again elsewhere:

    The call to subjects.Count() will throw an ArgumentNullException, not a NullReferenceException. Count() is another extension method here, and assuming the implementation in System.Linq.Enumerable is being used, that’s documented (correctly) to throw an ArgumentNullException. Try it if you don’t believe me.

    EDIT: Making this easier…

    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:

    internal static void ThrowIfNull<T>(this T argument, string name)     where T : class {     if (argument == null)     {         throw new ArgumentNullException(name);     } } 

    The example method in the question can then become:

    public static bool HasFive<T>(this IEnumerable<T> subjects) {     subjects.ThrowIfNull('subjects');         return subjects.Count() == 5; } 

    Another alternative would be to write a version which checked the value and returned it like this:

    internal static T NullGuard<T>(this T argument, string name)     where T : class {     if (argument == null)     {         throw new ArgumentNullException(name);     }     return argument; } 

    You can then call it fluently:

    public static bool HasFive<T>(this IEnumerable<T> subjects) {     return subjects.NullGuard('subjects').Count() == 5; } 

    This is also helpful for copying parameters in constructors etc:

    public Person(string name, int age) {     this.name = name.NullGuard('name');     this.age = age; } 

    (You might want an overload without the argument name for places where it’s not important.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following method: public static int CountNonNullMembers<T>(this IEnumerable<T> enumerable) {
So lets say I have this interface: public interface IBox { public void setSize(int
Lets say I have this array, int[] numbers = {1, 3, 4, 9, 2};
Lets say I have an array like this: string [] Filelist = ... I
Lets say I have a string COLIN. The numeric value of this string would
This is a contrived example, but lets say I have declared objects: CustomObj fooObj;
Let's say I have an interceptor that looks smth like this: public class AuthorizationInterceptor
Let's say I have this code: if (md5($_POST[$foo['bar']]) == $somemd5) { doSomethingWith(md5($_POST[$foo['bar']]); } I
Let's say I have this model named Product with a field named brand .
I know python functions are virtual by default. Let's say I have this: class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.