C#的泛型方法解析

2019-12-30 15:03:36刘景俊

   泛型类在.NET的应用主要在集合类中,大多数集合类在System.Collections.Generic和System.Collections.ObjectModel类中。下面简单的介绍一种泛型集合类:

     (1).SynchronizedCollection:提供一个线程安全集合,其中包含泛型参数所指定类型的对象作为元素.


  [ComVisible(false)]
 public class SynchronizedCollection<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
 {
 /// <summary>
 /// 初始化 <see cref="T:System.Collections.Generic.SynchronizedCollection`1"/> 类的新实例。
 /// </summary>
 public SynchronizedCollection();
 /// <summary>
 /// 通过用于对线程安全集合的访问进行同步的对象来初始化 <see cref="T:System.Collections.Generic.SynchronizedCollection`1"/> 类的新实例。
 /// </summary>
 /// <param name="syncRoot">用于对线程安全集合的访问进行同步的对象。</param><exception cref="T:System.ArgumentNullException"><paramref name="syncRoot"/> 为 null。</exception>
 public SynchronizedCollection(object syncRoot);
 /// <summary>
 /// 使用指定的可枚举元素列表和用于对线程安全集合的访问进行同步的对象来初始化 <see cref="T:System.Collections.Generic.SynchronizedCollection`1"/> 类的新实例。
 /// </summary>
 /// <param name="syncRoot">用于对线程安全集合的访问进行同步的对象。</param><param name="list">用于初始化线程安全集合的元素的 <see cref="T:System.Collections.Generic.IEnumerable`1"/> 集合。</param><exception cref="T:System.ArgumentNullException"><paramref name="syncRoot"/> 或 <paramref name="list"/> 为 null。</exception>
 public SynchronizedCollection(object syncRoot, IEnumerable<T> list);
 /// <summary>
 /// 使用指定的元素数组和用于对线程安全集合的访问进行同步的对象来初始化 <see cref="T:System.Collections.Generic.SynchronizedCollection`1"/> 类的新实例。
 /// </summary>
 /// <param name="syncRoot">用于对线程安全集合的访问进行同步的对象。</param><param name="list">用于初始化线程安全集合的 <paramref name="T"/> 类型元素的 <see cref="T:System.Array"/>。</param><exception cref="T:System.ArgumentNullException"><paramref name="syncRoot"/> 或 <paramref name="list"/> 为 null。</exception>
 public SynchronizedCollection(object syncRoot, params T[] list);
 /// <summary>
 /// 将项添加到线程安全只读集合中。
 /// </summary>
 /// <param name="item">要添加到集合的元素。</param><exception cref="T:System.ArgumentException">设置的值为 null,或者不是集合的正确泛型类型 <paramref name="T"/>。</exception>
 public void Add(T item);
 /// <summary>
 /// 从集合中移除所有项。
 /// </summary>
 public void Clear();
 /// <summary>
 /// 从特定索引处开始,将集合中的元素复制到指定的数组。
 /// </summary>
 /// <param name="array">从集合中复制的 <paramref name="T "/>类型元素的目标 <see cref="T:System.Array"/>。</param><param name="index">复制开始时所在的数组中的从零开始的索引。</param>
 public void CopyTo(T[] array, int index);
 /// <summary>
 /// 确定集合是否包含具有特定值的元素。
 /// </summary>
 /// 
 /// <returns>
 /// 如果在集合中找到元素值,则为 true;否则为 false。
 /// </returns>
 /// <param name="item">要在集合中定位的对象。</param><exception cref="T:System.ArgumentException">设置的值为 null,或者不是集合的正确泛型类型 <paramref name="T"/>。</exception>
 public bool Contains(T item);
 /// <summary>
 /// 返回一个循环访问同步集合的枚举数。
 /// </summary>
 /// 
 /// <returns>
 /// 一个 <see cref="T:System.Collections.Generic.IEnumerator`1"/>,用于访问集合中存储的类型的对象。
 /// </returns>
 public IEnumerator<T> GetEnumerator();
 /// <summary>
 /// 返回某个值在集合中的第一个匹配项的索引。
 /// </summary>
 /// 
 /// <returns>
 /// 该值在集合中的第一个匹配项的从零开始的索引。
 /// </returns>
 /// <param name="item">从集合中移除所有项。</param><exception cref="T:System.ArgumentException">设置的值为 null,或者不是集合的正确泛型类型 <paramref name="T"/>。</exception>
 public int IndexOf(T item);
 /// <summary>
 /// 将一项插入集合中的指定索引处。
 /// </summary>
 /// <param name="index">要从集合中检索的元素的从零开始的索引。</param><param name="item">要作为元素插入到集合中的对象。</param><exception cref="T:System.ArgumentOutOfRangeException">指定的 <paramref name="index"/> 小于零或大于集合中的项数。</exception><exception cref="T:System.ArgumentException">设置的值为 null,或者不是集合的正确泛型类型 <paramref name="T"/>。</exception>
 public void Insert(int index, T item);
 /// <summary>
 /// 从集合中移除指定项的第一个匹配项。
 /// </summary>
 /// 
 /// <returns>
 /// 如果从集合中成功移除了项,则为 true;否则为 false。
 /// </returns>
 /// <param name="item">要从集合中移除的对象。</param>
 public bool Remove(T item);
 /// <summary>
 /// 从集合中移除指定索引处的项。
 /// </summary>
 /// <param name="index">要从集合中检索的元素的从零开始的索引。</param><exception cref="T:System.ArgumentOutOfRangeException">指定的 <paramref name="index"/> 小于零或大于集合中的项数。</exception>
 public void RemoveAt(int index);
 /// <summary>
 /// 从集合中移除所有项。
 /// </summary>
 protected virtual void ClearItems();
 /// <summary>
 /// 将一项插入集合中的指定索引处。
 /// </summary>
 /// <param name="index">集合中从零开始的索引,在此处插入对象。</param><param name="item">要插入到集合中的对象。</param><exception cref="T:System.ArgumentOutOfRangeException">指定的 <paramref name="index"/> 小于零或大于集合中的项数。</exception><exception cref="T:System.ArgumentException">设置的值为 null,或者不是集合的正确泛型类型 <paramref name="T"/>。</exception>
 protected virtual void InsertItem(int index, T item);
 /// <summary>
 /// 从集合中移除指定 <paramref name="index"/> 处的项。
 /// </summary>
 /// <param name="index">要从集合中检索的元素的从零开始的索引。</param><exception cref="T:System.ArgumentOutOfRangeException">指定的 <paramref name="index"/> 小于零或大于集合中的项数。</exception>
 protected virtual void RemoveItem(int index);
 /// <summary>
 /// 使用另一项替换指定索引处的项。
 /// </summary>
 /// <param name="index">要替换的对象的从零开始的索引。</param><param name="item">要替换的对象。</param><exception cref="T:System.ArgumentOutOfRangeException">指定的 <paramref name="index"/> 小于零或大于集合中的项数。</exception>
 protected virtual void SetItem(int index, T item);
 /// <summary>
 /// 返回一个循环访问同步集合的枚举数。
 /// </summary>
 /// 
 /// <returns>
 /// 一个 <see cref="T:System.Collections.Generic.IEnumerator`1"/>,用于访问集合中存储的类型的对象。
 /// </returns>
 IEnumerator IEnumerable.GetEnumerator();
 /// <summary>
 /// 从特定索引处开始,将集合中的元素复制到指定的数组。
 /// </summary>
 /// <param name="array">从集合中复制的 <paramref name="T"/> 类型元素的目标 <see cref="T:System.Array"/>。</param><param name="index">复制开始时所在的数组中的从零开始的索引。</param>
 void ICollection.CopyTo(Array array, int index);
 /// <summary>
 /// 向集合中添加一个元素。
 /// </summary>
 /// 
 /// <returns>
 /// 新元素的插入位置。
 /// </returns>
 /// <param name="value">要添加到集合中的对象。</param>
 int IList.Add(object value);
 /// <summary>
 /// 确定集合是否包含具有特定值的元素。
 /// </summary>
 /// 
 /// <returns>
 /// 如果在集合中找到元素 <paramref name="value"/>,则为 true;否则为 false。
 /// </returns>
 /// <param name="value">要在集合中定位的对象。</param><exception cref="T:System.ArgumentException"><paramref name="value"/> 不是集合所含类型的对象。</exception>
 bool IList.Contains(object value);
 /// <summary>
 /// 确定集合中某个元素的从零开始的索引。
 /// </summary>
 /// 
 /// <returns>
 /// 如果在集合中找到,则为 <paramref name="value"/> 的索引;否则为 -1。
 /// </returns>
 /// <param name="value">集合中要确定其索引的元素。</param>
 int IList.IndexOf(object value);
 /// <summary>
 /// 将某个对象插入到集合中的指定索引处。
 /// </summary>
 /// <param name="index">从零开始的索引,将在该位置插入 <paramref name="value"/>。</param><param name="value">要在集合中插入的对象。</param><exception cref="T:System.ArgumentOutOfRangeException">指定的 <paramref name="index"/> 小于零或大于集合中的项数。</exception><exception cref="T:System.ArgumentException">设置的 <paramref name="value"/> 为 null,或者不是集合的正确泛型类型 <paramref name="T"/>。</exception>
 void IList.Insert(int index, object value);
 /// <summary>
 /// 从集合中移除作为元素的指定对象的第一个匹配项。
 /// </summary>
 /// <param name="value">要从集合中移除的对象。</param>
 void IList.Remove(object value);
 }