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

  • Home
  • SEARCH
  • 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 7592825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:00:13+00:00 2026-05-30T21:00:13+00:00

An extension method expects an open generic IEnumerable. Should I check inside the method

  • 0

An extension method expects an open generic IEnumerable.

Should I check inside the method wether the list is null.

I have the opinion that a list must never be null but have count == 0.

How do you handle such a case?

UPDATE:

I forgot to mention that the method is a recursive method where the list is recursively called/passed of course.

  • 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. Editorial Team
    Editorial Team
    2026-05-30T21:00:14+00:00Added an answer on May 30, 2026 at 9:00 pm

    Yes you should check that. It’s also fairly common pattern to throw in such case, especially in LINQ:

    public static void MyExtension<T>(this IEnumerable<T> source)
    {
        if (source == null)
        {
            throw new ArgumentNullException("source");
        }
    
        // ...
    }
    

    Handling empty list is much easier than handling list instance that is null. Think about it in this way; list with no items is simply empty collection – fairly common case. List that is null… represents what? Exceptional situation, and should be handled as such.

    Update:

    I looked around on what Microsoft has to say about my speculations that throwing ANE is common pattern (as opposed to letting CLR throw NRE – which at points might be too vague) and it seems to be correct. We can find at ANE documentation page that:

    ArgumentNullException (…) is provided so that application code can differentiate between exceptions caused by null arguments and exceptions caused by arguments that are not null.

    And later, in framework design guidelines’ Exception Throwing section:

    Do report execution failures by throwing exceptions. If a member cannot successfully do what it is designed to do, that should be considered an execution failure and an exception should be thrown.

    In your case, since you mentioned your method must never accept null list argument, it is simple execution failure situation.

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

Sidebar

Related Questions

I currently have an extension Method which converts an IEnumerable of type Tab into
I have written an extension method for string manipulation. I'm confused what should I
If the LINQ Count() extension method is invoked on an IEnumerable<T> that has a
I have a HtmlHelper extension method that I would like to apply some logic
Imagine i have an extension method that operates of an interface of abstract objects:
I have an extension method on HttpRequest that I'm trying to use inline on
I have an extension method I'm trying to make generic, for message passing. public
I have created a Extension method for IQueryable class to convert list of a
Extension method is a really helpful feature that you can add a lot of
I have a simple extension method on the int type so I can do

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.