C# - LINQ Extension: NotOfType<T>()

By , 8/24/2015
(0 ratings)
This LINQ like method returns all elements that are not of a specific type.

Required Namespace
System.Collections
/// <summary>
/// Returns all elements which are not of a specific type or there derivates..
/// </summary>
/// <typeparam name="TRemove">The type that should not be included in the result.</typeparam>
/// <param name="source">The source list.</param>
/// <returns>A list of all items from <paramref name="source"/> which type is not <typeparamref name="TRemove"/>.</returns>
public static IEnumerable NotOfType<TRemove>(this IEnumerable source)
{
    if (source == null)
    {
        throw new ArgumentNullException("source");
    }
    foreach (var item in source)
    {
        if (!(item is TRemove))
        {
            yield return item;
        }
    }
}
Tagged with LINQ, Extension, TypeOf, NotTypeOf.

Comments

 

Log in, to comment!