Why doesn't Any() work on a c# null object

.net, c#, ienumerable, object

Solution

When dealing with reference types, a `null` value is semantically different from an "empty" value.

A `null` string is not the same as `string.Empty`, and a `null` `IEnumerable<T>` is not the same as `Enumerable.Empty<T>` (or any other "empty" enumerable of that type).

If `Any` were not an extension method, calling it on `null` would result in `NullReferenceException`. Since it is an extension method, throwing some exception (although not necessary) is a good idea because it preserves the well-known semantics of trying to call a method on `null`: BOOM!

Problem

When calling Any() on a null object, it throws an ArgumentNullException in C#. If the object is null, there definitely aren't 'any', and it should probably return false. Why does C# behave this way?

Original source

Related problems