In the course of writing applications, a common scenario is where you need to check if a collection of any kind contains elements.

Take, for example, the following:

string[] people = ["Peter", "Bartholomew", "Matthew", "Simon", "Jude"];

One way to check is as follows:

if (people.Length > 0)
{
	Console.WriteLine("There are elements");
}

Here, we are outright checking the length of the collection. In this case, the array structure has a Length property.

Another way is as follows:

if (people.Any())
{
	Console.WriteLine("There are elements");
}

Here we are using the LINQ extension method Any to check if there are any elements.

They may return the same result, but they are not the same thing!

Length makes use of the fact that the array Length is always known in advance, whereas Any() tries to iterate through the collection and returns as soon as it is successful.

Not all collections store their lengths, and thus, it may be potentially expensive to determine emptiness using this technique.

In fact, it is possible to have an infinite collection (that makes use of yield), and thus attempting to count the elements may not succeed.

TLDR

It is advisable to use Any() rather than Count / Length to determine the emptiness of a collection.

Happy hacking