'In' Extension Method

by Donovan Olivier May 17, 2011 10:36AM

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: ,

Blog

Comments

May 26, 2011 4:42PM #

BJS

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 United States |

May 26, 2011 8:31PM #

donovan

params T[] is a good option if your list is known at design time.

donovan United States |

Comments are closed

The Perpetrator

perpetrator

Donovan Olivier

software developer, architect, husband, father, son, brother, uncle, nephew, sinistral, enquirer, skeptic, cynic, anarchist, contrarian, infidel, dissident, rebel, troublemaker, dissenter, renegade.

Noise

damn this winter appetite!
planetdonovan 1 day ago

@planetdonovan it seems to be the standard ratio!
marceldupreez 1 day ago

@marceldupreez cuz there's one in every carriage ;)
planetdonovan 1 day ago

RT @marclottering: Feel like getting stoned proper today. Anyone have a COSATU connection?
planetdonovan 2 days ago

fail of the day http://t.co/7l0RI9lr
planetdonovan 4 days ago

currently suffering from severe malbeertrition.
planetdonovan 7 days ago