Created
January 5, 2023 18:53
-
-
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()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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