-1

My understanding is nothing will happen.

For instance this code:

foreach (var some in (from u in possiblyNullCollection ) ) 
{
    // 
}

Should be guarded as:

if ( possiblyNullCollection != null ) 
{ 
    foreach (var some in (from u in possiblyNullCollection ) ) 
    {
     // 
    }
}

Or is it safe to query a null collection?

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • 10
    ...Have you tried it? – Adam Robinson Mar 28 '12 at 01:24
  • 1
    Takes like 1 minute to try this out – BrokenGlass Mar 28 '12 at 01:26
  • 1
    Related to... [Why does .NET foreach loop throw NullRefException when collection is null?](http://stackoverflow.com/questions/3088147/why-does-net-foreach-loop-throw-nullrefexception-when-collection-is-null) – M.Babcock Mar 28 '12 at 01:28
  • 1
    ... or a minute to research the many resources online that complain when it doesn't work. [Also related](http://stackoverflow.com/questions/644715/c-sharp-code-simplification-query-the-null-container-and-the-foreach-loop) – M.Babcock Mar 28 '12 at 01:29
  • [This one](http://stackoverflow.com/questions/6455311/is-ifitems-null-superfluous-before-foreacht-item-in-items) is likely a duplicate (this should probably be a faq...) – M.Babcock Mar 28 '12 at 01:34
  • I don't get why the downvotes, after all this is a legit question. If it is so trivial a simple < 1m answer would do. – OscarRyz Mar 28 '12 at 03:02
  • Anyway, I've got my answer and I voted to have it closed. – OscarRyz Mar 28 '12 at 03:03
  • 1
    @OscarRyz: It's not a legit question because it would've taken less time to try it yourself than it took for you to post the question. A good stackoverflow question is well defined, and presents what has already been tried and why those things didn't solve the problem. Nothing personal, but this type of question comes across as "Hey, I don't feel like trying this. Could someone do it for me." Note that the issue itself is a valid one. Many people feel that these functions should not throw an exception in these cases. But the answer about if they do or not is right in front of you. – Jonathan Wood Mar 30 '15 at 17:04

1 Answers1

19

A null collection will throw an exception if you query it with LINQ. You need to check for null.

Empty collections are fine however.

Something to keep in mind is that it's generally considered bad practice for collections to be null. Similar to having null items in a collection, it can cause a lot of bugs.

LINQPad Window showing the results of the query

Dan Rigby
  • 17,133
  • 6
  • 43
  • 60
  • What's the common idiom to handle this? To check it as in my second sample? – OscarRyz Mar 28 '12 at 01:45
  • 7
    Usually the best way to handle it is to not allow the collection to be null to begin with. If it's a class field, initialize it to an empty collection. If it's a method parameter, check it for null at the beginning of the method, and throw an ArgumentNullException if it is. Assuming you have to deal with null collections for whatever reason, you can wrap the foreach in an if block like you did, or you can use the null coalesce operator in the foreach block like this `foreach (var some in (from u in (possiblyNullCollection ?? Enumerable.Empty())))` where T is the type of the item. – Dan Rigby Mar 28 '12 at 01:51