📄 readonlycollectionbase.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>
/// ReadOnlyCollectionBase is a base class that can be used to more easily implement the
/// generic ICollection<T> and non-generic ICollection interfaces for a read-only collection:
/// a collection that does not allow adding or removing elements.
/// </summary>
/// <remarks>
/// <para>To use ReadOnlyCollectionBase as a base class, the derived class must override
/// the Count and GetEnumerator methods. </para>
/// <para>ICollection<T>.Contains need not be implemented by the
/// derived class, but it should be strongly considered, because the ReadOnlyCollectionBase implementation
/// may not be very efficient.</para>
/// </remarks>
/// <typeparam name="T">The item type of the collection.</typeparam>
[Serializable]
[DebuggerDisplay("{DebuggerDisplayString()}")]
public abstract class ReadOnlyCollectionBase<T> : ICollection<T>, ICollection
{
/// <summary>
/// Creates a new ReadOnlyCollectionBase.
/// </summary>
protected ReadOnlyCollectionBase()
{
}
/// <summary>
/// Throws an NotSupportedException stating that this collection cannot be modified.
/// </summary>
private void MethodModifiesCollection()
{
throw new NotSupportedException(string.Format(Strings.CannotModifyCollection, Util.SimpleClassName(this.GetType())));
}
#region Delegate operations
/// <summary>
/// Shows the string representation of the collection. The string representation contains
/// a list of the items in the collection.
/// </summary>
/// <returns>The string representation of the collection.</returns>
public override string ToString()
{
return Algorithms.ToString(this);
}
/// <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
/// defined by <paramref name="predicate"/>.
/// </summary>
/// <param name="predicate">A delegate that defines the condition to check for.</param>
/// <returns>True if all of the items in the collection satisfy the condition
/// defined by <paramref name="predicate"/>, or if the collection is empty. False if one or more items
/// in the collection do not satisfy <paramref name="predicate"/>.</returns>
public virtual bool TrueForAll(Predicate<T> predicate)
{
if (predicate == null)
throw new ArgumentNullException("predicate");
return Algorithms.TrueForAll(this, predicate);
}
/// <summary>
/// Counts the number of items in the collection that satisfy the condition
/// defined by <paramref name="predicate"/>.
/// </summary>
/// <param name="predicate">A delegate that defines the condition to check for.</param>
/// <returns>The number of items in the collection that satisfy <paramref name="predicate"/>.</returns>
public virtual int CountWhere(Predicate<T> predicate)
{
if (predicate == null)
throw new ArgumentNullException("predicate");
return Algorithms.CountWhere(this, predicate);
}
/// <summary>
/// Enumerates the items in the collection that satisfy the condition defined
/// by <paramref name="predicate"/>.
/// </summary>
/// <param name="predicate">A delegate that defines the condition to check for.</param>
/// <returns>An IEnumerable<T> that enumerates the items that satisfy the condition.</returns>
public IEnumerable<T> FindAll(Predicate<T> predicate)
{
if (predicate == null)
throw new ArgumentNullException("predicate");
return Algorithms.FindWhere(this, predicate);
}
/// <summary>
/// Performs the specified action on each item in this collection.
/// </summary>
/// <param name="action">An Action delegate which is invoked for each item in this collection.</param>
public virtual void ForEach(Action<T> action)
{
if (action == null)
throw new ArgumentNullException("action");
Algorithms.ForEach(this, action);
}
/// <summary>
/// Convert this collection of items by applying a delegate to each item in the collection. The resulting enumeration
/// contains the result of applying <paramref name="converter"/> to each item in this collection, in
/// order.
/// </summary>
/// <typeparam name="TOutput">The type each item is being converted to.</typeparam>
/// <param name="converter">A delegate to the method to call, passing each item in this collection.</param>
/// <returns>An IEnumerable<TOutput^gt; that enumerates the resulting collection from applying <paramref name="converter"/> to each item in this collection in
/// order.</returns>
/// <exception cref="ArgumentNullException"><paramref name="converter"/> is null.</exception>
public virtual IEnumerable<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter)
{
if (converter == null)
throw new ArgumentNullException("converter");
return Algorithms.Convert(this, converter);
}
#endregion Delegate operations
#region ICollection<T> Members
/// <summary>
/// This method throws an NotSupportedException
/// stating the collection is read-only.
/// </summary>
/// <param name="item">Item to be added to the collection.</param>
/// <exception cref="NotSupportedException">Always thrown.</exception>
void ICollection<T>.Add(T item)
{
MethodModifiesCollection();
}
/// <summary>
/// This method throws an NotSupportedException
/// stating the collection is read-only.
/// </summary>
/// <exception cref="NotSupportedException">Always thrown.</exception>
void ICollection<T>.Clear()
{
MethodModifiesCollection();
}
/// <summary>
/// This method throws an NotSupportedException
/// stating the collection is read-only.
/// </summary>
/// <param name="item">Item to be removed from the collection.</param>
/// <exception cref="NotSupportedException">Always thrown.</exception>
bool ICollection<T>.Remove(T item)
{
MethodModifiesCollection();
return false;
}
/// <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.</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)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -