The exact details differ a bit in .NET Framework vs .NET Core, but it also somewhat depends on what you're doing: if you're using an ICollection or ICollection<T> type (such as with List<T>) there is a .Count property that's cheap to access, whereas other types might require enumeration.
TL;DR:
Use .Count > 0 if the property exists, and otherwise .Any().
Using .Count() > 0 is never the best option, and in some cases could be dramatically slower.
This applies to both .NET Framework and .NET Core.
Now we can dive into the details..
Lists and Collections
Let's start with a very common case: using List<T> (which is also ICollection<T>).
The .Count property is implemented as:
private int _size;
public int Count {
get {
Contract.Ensures(Contract.Result<int>() >= 0);
return _size;
}
}
What this is saying is _size is maintained by Add(),Remove() etc, and since it's just accessing a field this is an extremely cheap operation -- we don't need to iterate over values.
ICollection and ICollection<T> both have .Count and most types that implement them are likely to do so in a similar way.
Other IEnumerables
Any other IEnumerable types that aren't also ICollection require starting enumeration to determine if they're empty or not. The key factor affecting performance is if we end up enumerating a single item (ideal) or the entire collection (relatively expensive).
If the collection is actually causing I/O such as by reading from a database or disk, this could be a big performance hit.
.NET Framework .Any()
In .NET Framework (4.8), the Any() implementation is:
public static bool Any<TSource>(this IEnumerable<TSource> source) {
if (source == null) throw Error.ArgumentNull("source");
using (IEnumerator<TSource> e = source.GetEnumerator()) {
if (e.MoveNext()) return true;
}
return false;
}
This means no matter what, it's going to get a new enumerator object and try iterating once. This is more expensive than calling the List<T>.Count property, but at least it's not iterating the entire list.
.NET Framework .Count()
In .NET Framework (4.8), the Count() implementation is (basically):
public static int Count<TSource>(this IEnumerable<TSource> source)
{
ICollection<TSource> collection = source as ICollection<TSource>;
if (collection != null)
{
return collection.Count;
}
int num = 0;
using (IEnumerator<TSource> enumerator = source.GetEnumerator())
{
while (enumerator.MoveNext())
{
num = checked(num + 1);
}
return num;
}
}
If available, ICollection.Count is used, but otherwise the collection is enumerated.
.NET Core .Any()
The LINQ Any() implementation in .NET Core is much smarter. You can see the complete source here but the relevant bits to this discussion:
public static bool Any<TSource>(this IEnumerable<TSource> source)
{
//..snip..
if (source is ICollection<TSource> collectionoft)
{
return collectionoft.Count != 0;
}
//..snip..
using (IEnumerator<TSource> e = source.GetEnumerator())
{
return e.MoveNext();
}
}
Because a List<T> is an ICollection<T>, this will call the Count property (and though it calls another method, there's no extra allocations).
.NET Core .Count()
The .NET Core implementation (source) is basically the same as .NET Framework (see above), and so it will use ICollection.Count if available, and otherwise enumerates the collection.
Summary
.NET Framework
.NET Core
.Count > 0 is best, if available (ICollection)
.Any() is fine, and will either do ICollection.Count > 0 or enumerate a single item
.Count() > 0 is bad as it causes complete enumeration