IEnumerable接口的扩展方法

/// <summary>
/// IEnumerable接口的扩展方法,支持它的实现类是List的情况
/// </summary>
using System.Collections.Generic;
public static class IEnumerableExtensions
{
/// <summary>
/// 向集合中添加元素
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="collection"></param>
/// <param name="value"></param>
public static void Add<T>(this IEnumerable<T> collection, T value)
{
(collection as List<T>).Add(value);
}
/// <summary>
/// 从集合中删除元素
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="collection"></param>
/// <param name="value"></param>
public static void Remove<T>(this IEnumerable<T> collection, T value)
{
(collection as List<T>).Remove(value);
}
/// <summary>
/// 检索集合中是否包含某个元素
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="collection"></param>
/// <param name="value"></param>
/// <returns></returns>
public static bool Contains<T>(this IEnumerable<T> collection, T value)
{
return (collection as List<T>).Contains(value);
}
}

posted @ 2016-06-22 18:05  那就让我这样吧  阅读(139)  评论(0编辑  收藏  举报