📄 collectionbase.cs
字号:
/// 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 virtual IEnumerable<T> FindAll(Predicate<T> predicate)
{
if (predicate == null)
throw new ArgumentNullException("predicate");
return Algorithms.FindWhere(this, predicate);
}
/// <summary>
/// Removes all 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>Returns a collection of the items that were removed, in sorted order.</returns>
public virtual ICollection<T> RemoveAll(Predicate<T> predicate)
{
if (predicate == null)
throw new ArgumentNullException("predicate");
return Algorithms.RemoveWhere(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
#region IEnumerable<T> Members
/// <summary>
/// Must be overridden to enumerate all the members of the collection.
/// </summary>
/// <returns>A generic IEnumerator<T> that can be used
/// to enumerate all the items in the collection.</returns>
public abstract IEnumerator<T> GetEnumerator();
#endregion
#region ICollection Members
/// <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="index">Starting index in <paramref name="array"/> to copy to.</param>
void ICollection.CopyTo(Array array, int index)
{
int count = this.Count;
if (count == 0)
return;
if (array == null)
throw new ArgumentNullException("array");
if (index < 0)
throw new ArgumentOutOfRangeException("index", index, Strings.ArgMustNotBeNegative);
if (index >= array.Length || count > array.Length - index)
throw new ArgumentException("index", Strings.ArrayTooSmall);
int i = 0;
foreach (object o in (ICollection)this) {
if (i >= count)
break;
array.SetValue(o, index);
++index;
++i;
}
}
/// <summary>
/// Indicates whether the collection is synchronized.
/// </summary>
/// <value>Always returns false, indicating that the collection is not synchronized.</value>
bool ICollection.IsSynchronized
{
get { return false; }
}
/// <summary>
/// Indicates the synchronization object for this collection.
/// </summary>
/// <value>Always returns this.</value>
object ICollection.SyncRoot
{
get { return this; }
}
#endregion
#region IEnumerable Members
/// <summary>
/// Provides an IEnumerator that can be used to iterate all the members of the
/// collection. This implementation uses the IEnumerator<T> that was overridden
/// by the derived classes to enumerate the members of the collection.
/// </summary>
/// <returns>An IEnumerator that can be used to iterate the collection.</returns>
IEnumerator IEnumerable.GetEnumerator()
{
foreach (T item in this) {
yield return item;
}
}
#endregion
/// <summary>
/// Display the contents of the collection in the debugger. This is intentionally private, it is called
/// only from the debugger due to the presence of the DebuggerDisplay attribute. It is similar
/// format to ToString(), but is limited to 250-300 characters or so, so as not to overload the debugger.
/// </summary>
/// <returns>The string representation of the items in the collection, similar in format to ToString().</returns>
internal string DebuggerDisplayString()
{
const int MAXLENGTH = 250;
System.Text.StringBuilder builder = new System.Text.StringBuilder();
builder.Append('{');
// Call ToString on each item and put it in.
bool firstItem = true;
foreach (T item in this) {
if (builder.Length >= MAXLENGTH) {
builder.Append(",...");
break;
}
if (!firstItem)
builder.Append(',');
if (item == null)
builder.Append("null");
else
builder.Append(item.ToString());
firstItem = false;
}
builder.Append('}');
return builder.ToString();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -