planetdonovan.com
In the age old tradition of useless blog posts, here's an extension method that provides an alternate syntax for checking if an item is contained in a list:
public static bool In<T>(this T item, IEnumerable<T> list) { return list.Contains(item); }
Tags: C#, Extension Methods
Blog
Related posts
Comments
May 26, 2011 4:42PM #
Funny. I just wrote something like this a few days ago. I used params T[] though instead of IEnumerable<T>. That way the syntax flows a little more like SQL. In any case, I was thinking about it's performance today and started Googling. I was wondering how this might perform if someone decided to do an IN against 1,000 items or something and wanted to see what others might have done. Your result was near the top in a query for: in extension method.
BJS |
May 26, 2011 8:31PM #
params T[] is a good option if your list is known at design time.
donovan |
Powered by BlogEngine.NET 1.6.1.0 Base theme by Mads Kristensen This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.
Donovan Olivier
software developer, architect, husband, father, son, brother, uncle, nephew, sinistral, enquirer, skeptic, cynic, anarchist, contrarian, infidel, dissident, rebel, troublemaker, dissenter, renegade.