In This Article

SimpleCollection<T> Class

Provides a generic collection with virtual methods to respond to and validate items being added or removed.

public class SimpleCollection<T> : Collection<T> where T : class
Type Parameters:
T -

The type of collection item.

Inheritance:
object Collection<T> object
Derived:
BarCommandCollection BarKeyboardShortcutCollection BarManagerDockableToolBarCollection BarModeCollection PopupMenuCollection

Constructors

SimpleCollection()

Initializes an instance of the class.

public SimpleCollection()

SimpleCollection(List<T>)

Initializes an instance of the class as a wrapper for the specified list.

public SimpleCollection(List<T> list)
Parameter Type Description
list List<T>

Properties

DefaultComparer

Defines the default comparer to be used when sorting. If an explicit default is not defined, an implicit default will be used.

protected virtual IComparer<T>? DefaultComparer { get; }

Property Value

IComparer<T>

Methods

AddRange(IEnumerable<T>)

Adds the elements of the specified collection to the end of this collection.

public void AddRange(IEnumerable<T> collection)
Parameter Type Description
collection IEnumerable<T>

The collection whose elements should be added.

ClearItems()

Removes all elements from the Collection<T>.

protected override sealed void ClearItems()

FindIndex(int, int, Predicate<T>)

Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the List<T> that starts at the specified index and contains the specified number of elements.

public int FindIndex(int startIndex, int count, Predicate<T> match)
Parameter Type Description
startIndex int

The zero-based starting index of the search.

count int

The number of elements in the section to search.

match Predicate<T>

The Predicate<T> delegate that defines the conditions of the element to search for.

Returns

int:

The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

Exceptions

Type Condition
ArgumentNullException

match is null.

ArgumentOutOfRangeException
   <code class="paramref">startIndex</code> is outside the range of valid indexes for the <xref href="System.Collections.Generic.List%601" data-throw-if-not-resolved="false"></xref>.  

-or-

count is less than 0.

-or-

startIndex and count do not specify a valid section in the List<T>.

FindIndex(int, Predicate<T>)

Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the List<T> that extends from the specified index to the last element.

public int FindIndex(int startIndex, Predicate<T> match)
Parameter Type Description
startIndex int

The zero-based starting index of the search.

match Predicate<T>

The Predicate<T> delegate that defines the conditions of the element to search for.

Returns

int:

The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

Exceptions

Type Condition
ArgumentNullException

match is null.

ArgumentOutOfRangeException

startIndex is outside the range of valid indexes for the List<T>.

FindIndex(Predicate<T>)

Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the entire List<T>.

public int FindIndex(Predicate<T> match)
Parameter Type Description
match Predicate<T>

The Predicate<T> delegate that defines the conditions of the element to search for.

Returns

int:

The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

Exceptions

Type Condition
ArgumentNullException

match is null.

FindLastIndex(int, int, Predicate<T>)

Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the range of elements in the List<T> that contains the specified number of elements and ends at the specified index.

public int FindLastIndex(int startIndex, int count, Predicate<T> match)
Parameter Type Description
startIndex int

The zero-based starting index of the backward search.

count int

The number of elements in the section to search.

match Predicate<T>

The Predicate<T> delegate that defines the conditions of the element to search for.

Returns

int:

The zero-based index of the last occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

Exceptions

Type Condition
ArgumentNullException

match is null.

ArgumentOutOfRangeException
   <code class="paramref">startIndex</code> is outside the range of valid indexes for the <xref href="System.Collections.Generic.List%601" data-throw-if-not-resolved="false"></xref>.  

-or-

count is less than 0.

-or-

startIndex and count do not specify a valid section in the List<T>.

FindLastIndex(int, Predicate<T>)

Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the range of elements in the List<T> that extends from the first element to the specified index.

public int FindLastIndex(int startIndex, Predicate<T> match)
Parameter Type Description
startIndex int

The zero-based starting index of the backward search.

match Predicate<T>

The Predicate<T> delegate that defines the conditions of the element to search for.

Returns

int:

The zero-based index of the last occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

Exceptions

Type Condition
ArgumentNullException

match is null.

ArgumentOutOfRangeException

startIndex is outside the range of valid indexes for the List<T>.

ForEach(Action<T>)

Performs the specified action on each element of the List<T>.

public void ForEach(Action<T> action)
Parameter Type Description
action Action<T>

The Action<T> delegate to perform on each element of the List<T>.

Exceptions

Type Condition
ArgumentNullException

action is null.

InvalidOperationException

An element in the collection has been modified.

IndexOf(T, int)

Searches for the specified object and returns the zero-based index of the first occurrence within the range of elements in the List<T> that extends from the specified index to the last element.

public int IndexOf(T item, int index)
Parameter Type Description
item T

The object to locate in the List<T>. The value can be null for reference types.

index int

The zero-based starting index of the search. 0 (zero) is valid in an empty list.

Returns

int:

The zero-based index of the first occurrence of item within the range of elements in the List<T> that extends from index to the last element, if found; otherwise, -1.

Exceptions

Type Condition
ArgumentOutOfRangeException

index is outside the range of valid indexes for the List<T>.

IndexOf(T, int, int)

Searches for the specified object and returns the zero-based index of the first occurrence within the range of elements in the List<T> that starts at the specified index and contains the specified number of elements.

public int IndexOf(T item, int index, int count)
Parameter Type Description
item T

The object to locate in the List<T>. The value can be null for reference types.

index int

The zero-based starting index of the search. 0 (zero) is valid in an empty list.

count int

The number of elements in the section to search.

Returns

int:

The zero-based index of the first occurrence of item within the range of elements in the List<T> that starts at index and contains count number of elements, if found; otherwise, -1.

Exceptions

Type Condition
ArgumentOutOfRangeException
   <code class="paramref">index</code> is outside the range of valid indexes for the <xref href="System.Collections.Generic.List%601" data-throw-if-not-resolved="false"></xref>.  

-or-

count is less than 0.

-or-

index and count do not specify a valid section in the List<T>.

InsertItem(int, T)

Inserts an element into the Collection<T> at the specified index.

protected override sealed void InsertItem(int index, T item)
Parameter Type Description
index int

The zero-based index at which item should be inserted.

item T

The object to insert. The value can be null for reference types.

Exceptions

Type Condition
ArgumentOutOfRangeException
   <code class="paramref">index</code> is less than zero.  

-or-

index is greater than Count.

InsertRange(int, IEnumerable<T>)

Inserts the elements of the specified collection into the collection starting at the specified index.

public void InsertRange(int index, IEnumerable<T> collection)
Parameter Type Description
index int

The zero-based index at which the new elements should be inserted.

collection IEnumerable<T>

The collection whose elements should be added.

LastIndexOf(T)

Searches for the specified object and returns the zero-based index of the last occurrence within the entire List<T>.

public int LastIndexOf(T item)
Parameter Type Description
item T

The object to locate in the List<T>. The value can be null for reference types.

Returns

int:

The zero-based index of the last occurrence of item within the entire the List<T>, if found; otherwise, -1.

LastIndexOf(T, int)

Searches for the specified object and returns the zero-based index of the last occurrence within the range of elements in the List<T> that extends from the first element to the specified index.

public int LastIndexOf(T item, int index)
Parameter Type Description
item T

The object to locate in the List<T>. The value can be null for reference types.

index int

The zero-based starting index of the backward search.

Returns

int:

The zero-based index of the last occurrence of item within the range of elements in the List<T> that extends from the first element to index, if found; otherwise, -1.

Exceptions

Type Condition
ArgumentOutOfRangeException

index is outside the range of valid indexes for the List<T>.

OnItemAdded(int, T)

Performs additional custom processes after an item is added to the collection.

protected virtual void OnItemAdded(int index, T value)
Parameter Type Description
index int

The zero-based index at which the item was inserted.

value T

The object that was inserted.

Remarks

The default implementation of this method is intended to be overridden by a derived class to perform some action after the specified object is added.

OnItemAdding(int, T)

Performs additional custom processes before an item is added to the collection.

protected virtual void OnItemAdding(int index, T value)
Parameter Type Description
index int

The zero-based index at which the item was inserted.

value T

The object that was inserted.

Remarks

The default implementation of this method is intended to be overridden by a derived class to perform some action after the specified object is added.

OnItemRemoved(int, T)

Performs additional custom processes after an item is removed from the collection.

protected virtual void OnItemRemoved(int index, T value)
Parameter Type Description
index int

The zero-based index from which the item was removed.

value T

The object that was removed.

Remarks

The default implementation of this method is intended to be overridden by a derived class to perform some action after the specified object is removed.

OnItemRemoving(int, T)

Performs additional custom processes before an item is removed from the collection.

protected virtual void OnItemRemoving(int index, T value)
Parameter Type Description
index int

The zero-based index from which the item was removed.

value T

The object that was removed.

Remarks

The default implementation of this method is intended to be overridden by a derived class to perform some action before the specified object is removed.

OnValidate(T, int)

Performs additional custom processes when validating a value.

protected virtual void OnValidate(T value, int existingIndex)
Parameter Type Description
value T

The object to validate.

existingIndex int

The index within the collection of the object that would be replaced by the value.

Remarks

The default implementation of this method determines whether value is null, and if so, throws ArgumentNullException. It is intended to be overridden by a derived class to perform additional action when the specified element is validated.

RemoveItem(int)

Removes the element at the specified index of the Collection<T>.

protected override void RemoveItem(int index)
Parameter Type Description
index int

The zero-based index of the element to remove.

Exceptions

Type Condition
ArgumentOutOfRangeException
   <code class="paramref">index</code> is less than zero.  

-or-

index is equal to or greater than Count.

SetItem(int, T)

Replaces the element at the specified index.

protected override void SetItem(int index, T item)
Parameter Type Description
index int

The zero-based index of the element to replace.

item T

The new value for the element at the specified index. The value can be null for reference types.

Exceptions

Type Condition
ArgumentOutOfRangeException
   <code class="paramref">index</code> is less than zero.  

-or-

index is greater than Count.

Sort()

Sorts the elements in the entire List<T> using the default comparer.

public void Sort()

Exceptions

Type Condition
InvalidOperationException

The default comparer Default cannot find an implementation of the IComparable<T> generic interface or the IComparable interface for type T.

Sort(IComparer<T>)

Sorts the elements in the entire List<T> using the specified comparer.

public void Sort(IComparer<T> comparer)
Parameter Type Description
comparer IComparer<T>

The IComparer<T> implementation to use when comparing elements, or null to use the default comparer Default.

Exceptions

Type Condition
InvalidOperationException

comparer is null, and the default comparer Default cannot find implementation of the IComparable<T> generic interface or the IComparable interface for type T.

ArgumentException

The implementation of comparer caused an error during the sort. For example, comparer might not return 0 when comparing an item with itself.

Sort(Comparison<T>)

Sorts the elements in the entire List<T> using the specified Comparison<T>.

public void Sort(Comparison<T> comparison)
Parameter Type Description
comparison Comparison<T>

The Comparison<T> to use when comparing elements.

Exceptions

Type Condition
ArgumentNullException

comparison is null.

ArgumentException

The implementation of comparison caused an error during the sort. For example, comparison might not return 0 when comparing an item with itself.

Inherited Members

Extension Methods