Skip to content

Instantly share code, notes, and snippets.

@rquackenbush
Created January 5, 2023 18:53
Show Gist options
  • Save rquackenbush/023b6a0ea5c01a4151a8cf030feacae0 to your computer and use it in GitHub Desktop.
Save rquackenbush/023b6a0ea5c01a4151a8cf030feacae0 to your computer and use it in GitHub Desktop.
Take an IEnumerable<T> and cast to IList<T> (if possible), otherwise materialize the list with .ToList()
namespace ConsoleApp1
{
public static class IEnumerableExtensions
{
public static IList<T> CastOrMaterializeList<T>(this IEnumerable<T> source)
{
if (source is IList<T>)
return (IList<T>)source;
return source
.ToList();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment