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 7508885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:45:43+00:00 2026-05-29T22:45:43+00:00

Unsurprisingly , following code will throw an ArgumentNullException IEnumerable<string> collection = null; string[] collectionViewAsAnArray

  • 0

Unsurprisingly, following code will throw an ArgumentNullException

IEnumerable<string> collection = null;
string[] collectionViewAsAnArray = collection.ToArray();

This looks obvious at first sight … but ain’t incoherent to argue that returning null may have been a reasonnable alternative (Accounting that ToArray() is an extension method and therefor can be called, even on null).

While I acknowledge, this way, extension behave like a real method, I can’t help finding the other approach really smart too … but this may lead to other issues?

  • 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-29T22:45:44+00:00Added an answer on May 29, 2026 at 10:45 pm

    Why push the error to some other spot in your code? If you expect that something can be null, check it before you use it. Otherwise throw an error exactly at the spot where your assumption was false, namely when you tried to convert it to an array.

    I understand it can be debatable when you’re chaining operations but in those cases I’ve found it easier to work with an empty IEnumerable rather than a null one.

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

Sidebar

Related Questions

I've developed some concept code for a project that I will be working on
I have a string class that, unsurprisingly, uses a different implementation depending on whether
I'm testing the following code: #include <iostream> #include <vector> #include <algorithm> #include <ctime> int
What it says above. Unsurprisingly, I couldn't find a clear answer to this. I
I'm trying to learn assembly (Intel x86 with NASM on Linux) and unsurprisingly I'm
Suppose we have a function MutateX which will mutate a mutable object x (in
I have installed the developer tools. I can compile code via Xcode and according
Possible Duplicate: Varying behavior for possible loss of precision Code Sample A public class
SQLServer int field. Value sometimes null. DataAdapter fills dataset OK and can display data
Simplified version of the code: Foo.h: class Foo { private: class Bar { //

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.