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

Ask A Question

Stats

  • Questions 112k
  • Answers 112k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer UL stands for unordered list. Use it for lists of… May 11, 2026 at 9:52 pm
  • Editorial Team
    Editorial Team added an answer You need a download script that serves larger files in… May 11, 2026 at 9:52 pm
  • Editorial Team
    Editorial Team added an answer Usually you do something like this, Client-side: <div id='Uploadcontainer'> <input… May 11, 2026 at 9:52 pm

Related Questions

Lets say I have something called Stuff in my database, with a property called
Inspired by the MVC storefront the latest project I'm working on is using extension
Let's say I create a class with a property: public class User { private
I'm trying to use the Html.DropDownList extension method but can't figure out how to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.