using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
public static void Main()
string[] array = new string[loops];
for (int i = 0; i < loops; i++)
List<string> list = new List<string>();
for (int i = 0; i < loops; i++)
IEnumerable<string> enumerableSelect = list.Select(t => t);
IReadOnlyCollection<string> readOnlyCollection = new ReadOnlyCollection<string>(list);
Stopwatch sw = new Stopwatch();
Stopwatch(enumerableSelect);
Stopwatch(readOnlyCollection);
void Stopwatch(IEnumerable<string> strings)
for (int i = 0; i < loops; i++)
Console.WriteLine($"{strings.GetType()} Any " + sw.Elapsed);
for (int i = 0; i < loops; i++)
array.NullOrEmptyCount();
Console.WriteLine($"{strings.GetType()} Count " + sw.Elapsed);
for (int i = 0; i < loops; i++)
Console.WriteLine($"{strings.GetType()} Ifs " + sw.Elapsed);
public static class EnumerableExtensions
public static bool NullOrEmpty<T>(this IEnumerable<T> enumerable) =>
enumerable == null || !enumerable.Any();
public static bool NullOrEmptyCount<T>(this IEnumerable<T> enumerable) =>
enumerable == null || enumerable.Count() == 0;
public static bool NullOrEmptyIfs<T>(this IEnumerable<T> enumerable)
if (enumerable is ICollection<T> collection)
return collection.Count == 0;
public static bool NotNullAndAny<T>(this IEnumerable<T> enumerable) =>
!enumerable.NullOrEmpty();
public static bool NotNullAndAny<T>(this IEnumerable<T> enumerable, Func<T, bool> predicate) => enumerable?.Any(predicate) ?? false;