IEnumerable接口的扩展方法

时间:2022-05-08 09:42:43

/// <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);
}
}