codeSMART

Entries tagged as ‘generics’

Generic List ContainsOneOf

July 14, 2008 · Leave a Comment

I needed a function that would return a boolean indicating if any of the values from one list appeared in another list.  I love extension methods…

public static bool ContainsOneOf<T>(this IList<T> first, IList<T> second)
{
    foreach (T item in first)
    {
        if (second.Contains(item))
            return true;
    }
    return false;
}

Remember to place this method in a public static class.

Categories: Coding
Tagged: , ,