using System.Collections.Generic;
public static void Main()
TestListPerformance(ListTest,10000);
TestListPerformance(YieldTest,10000);
public static void TestListPerformance(Func<int,IEnumerable<string>> func, int recordsToTest)
var watch = System.Diagnostics.Stopwatch.StartNew();
var results = func(recordsToTest);
foreach(var index in results)
Console.WriteLine(watch.ElapsedMilliseconds);
Console.WriteLine(watch.ElapsedTicks);
public static IEnumerable<string> ListTest(int totalRecords)
var list = new List<string>();
for(var i = 0; i < totalRecords; i++)
list.Add($"Hello World {i}");
public static IEnumerable<string> YieldTest(int totalRecords)
for(var i = 0; i < totalRecords; i++)
yield return $"Hello World {i}";
public static bool IsEmpty(object source)
var sourceType = source.GetType();
if (!sourceType.IsGenericType)
if (!sourceType.GetInterfaces().Any(t => t.GetGenericTypeDefinition() == typeof(IEnumerable<>)))
var childType = sourceType.GetGenericArguments()[0];
MethodInfo isAny = typeof(System.Linq.Enumerable)
.First(t => t.Name == nameof(System.Linq.Enumerable.Any) && t.IsGenericMethod)
.MakeGenericMethod(new Type[]{ childType });
if (!(bool)isAny.Invoke(null, new object[] { source }))