📄 collectionbase.cs
字号:
//******************************
// Written by Peter Golde
// Copyright (c) 2004-2005, Wintellect
//
// Use and restribution of this code is subject to the license agreement
// contained in the file "License.txt" accompanying this file.
//******************************
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace Wintellect.PowerCollections
{
/// <summary>
/// CollectionBase is a base class that can be used to more easily implement the
/// generic ICollection<T> and non-generic ICollection interfaces.
/// </summary>
/// <remarks>
/// <para>To use CollectionBase as a base class, the derived class must override
/// the Count, GetEnumerator, Add, Clear, and Remove methods. </para>
/// <para>ICollection<T>.Contains need not be implemented by the
/// derived class, but it should be strongly considered, because the CollectionBase implementation
/// may not be very efficient.</para>
/// </remarks>
/// <typeparam name="T">The item type of the collection.</typeparam>
[Serializable]
[DebuggerDisplay("{DebuggerDisplayString()}")]
public abstract class CollectionBase<T> : ICollection<T>, ICollection
{
/// <summary>
/// Creates a new CollectionBase.
/// </summary>
protected CollectionBase()
{
}
/// <summary>
/// Shows the string representation of the collection. The string representation contains
/// a list of the items in the collection. Contained collections (except string) are expanded
/// recursively.
/// </summary>
/// <returns>The string representation of the collection.</returns>
public override string ToString()
{
return Algorithms.ToString(this);
}
#region ICollection<T> Members
/// <summary>
/// Must be overridden to allow adding items to this collection.
/// </summary>
/// <remarks><p>This method is not abstract, although derived classes should always
/// override it. It is not abstract because some derived classes may wish to reimplement
/// Add with a different return type (typically bool). In C#, this can be accomplished
/// with code like the following:</p>
/// <code>
/// public class MyCollection<T>: CollectionBase<T>, ICollection<T>
/// {
/// public new bool Add(T item) {
/// /* Add the item */
/// }
///
/// void ICollection<T>.Add(T item) {
/// Add(item);
/// }
/// }
/// </code>
/// </remarks>
/// <param name="item">Item to be added to the collection.</param>
/// <exception cref="NotImplementedException">Always throws this exception to indicated
/// that the method must be overridden or re-implemented in the derived class.</exception>
public virtual void Add(T item)
{
throw new NotImplementedException(Strings.MustOverrideOrReimplement);
}
/// <summary>
/// Must be overridden to allow clearing this collection.
/// </summary>
public abstract void Clear();
/// <summary>
/// Must be overridden to allow removing items from this collection.
/// </summary>
/// <returns>True if <paramref name="item"/> existed in the collection and
/// was removed. False if <paramref name="item"/> did not exist in the collection.</returns>
public abstract bool Remove(T item);
/// <summary>
/// Determines if the collection contains a particular item. This default implementation
/// iterates all of the items in the collection via GetEnumerator, testing each item
/// against <paramref name="item"/> using IComparable<T>.Equals or
/// Object.Equals.
/// </summary>
/// <remarks>You should strongly consider overriding this method to provide
/// a more efficient implementation, or if the default equality comparison
/// is inappropriate.</remarks>
/// <param name="item">The item to check for in the collection.</param>
/// <returns>True if the collection contains <paramref name="item"/>, false otherwise.</returns>
public virtual bool Contains(T item)
{
IEqualityComparer<T> equalityComparer = EqualityComparer<T>.Default;
foreach (T i in this) {
if (equalityComparer.Equals(i, item))
return true;
}
return false;
}
/// <summary>
/// Copies all the items in the collection into an array. Implemented by
/// using the enumerator returned from GetEnumerator to get all the items
/// and copy them to the provided array.
/// </summary>
/// <param name="array">Array to copy to.</param>
/// <param name="arrayIndex">Starting index in <paramref name="array"/> to copy to.</param>
public virtual void CopyTo(T[] array, int arrayIndex)
{
int count = this.Count;
if (count == 0)
return;
if (array == null)
throw new ArgumentNullException("array");
if (count < 0)
throw new ArgumentOutOfRangeException("count", count, Strings.ArgMustNotBeNegative);
if (arrayIndex < 0)
throw new ArgumentOutOfRangeException("arrayIndex", arrayIndex, Strings.ArgMustNotBeNegative);
if (arrayIndex >= array.Length || count > array.Length - arrayIndex)
throw new ArgumentException("arrayIndex", Strings.ArrayTooSmall);
int index = arrayIndex, i = 0;
foreach (T item in (ICollection<T>)this) {
if (i >= count)
break;
array[index] = item;
++index;
++i;
}
}
/// <summary>
/// Creates an array of the correct size, and copies all the items in the
/// collection into the array, by calling CopyTo.
/// </summary>
/// <returns>An array containing all the elements in the collection, in order.</returns>
public virtual T[] ToArray()
{
int count = this.Count;
T[] array = new T[count];
CopyTo(array, 0);
return array;
}
/// <summary>
/// Must be overridden to provide the number of items in the collection.
/// </summary>
/// <value>The number of items in the collection.</value>
public abstract int Count { get; }
/// <summary>
/// Indicates whether the collection is read-only. Always returns false.
/// </summary>
/// <value>Always returns false.</value>
bool ICollection<T>.IsReadOnly
{
get { return false; }
}
/// <summary>
/// Provides a read-only view of this collection. The returned ICollection<T> provides
/// a view of the collection that prevents modifications to the collection. Use the method to provide
/// access to the collection without allowing changes. Since the returned object is just a view,
/// changes to the collection will be reflected in the view.
/// </summary>
/// <returns>An ICollection<T> that provides read-only access to the collection.</returns>
public virtual ICollection<T> AsReadOnly()
{
return Algorithms.ReadOnly<T>(this);
}
#endregion
#region Delegate operations
/// <summary>
/// Determines if the collection contains any item that satisfies the condition
/// defined by <paramref name="predicate"/>.
/// </summary>
/// <param name="predicate">A delegate that defines the condition to check for.</param>
/// <returns>True if the collection contains one or more items that satisfy the condition
/// defined by <paramref name="predicate"/>. False if the collection does not contain
/// an item that satisfies <paramref name="predicate"/>.</returns>
public virtual bool Exists(Predicate<T> predicate)
{
if (predicate == null)
throw new ArgumentNullException("predicate");
return Algorithms.Exists(this, predicate);
}
/// <summary>
/// Determines if all of the items in the collection satisfy the condition
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -