Table of Contents

Class MonitoredCollection<T>

Namespace
NanoByte.Common.Collections
Assembly
NanoByte.Common.dll

A collection that can easily be monitored for changes via events.

public class MonitoredCollection<T> : Collection<T>

Type Parameters

T

The type of elements in the collection.

Inheritance
object
Collection<T>
MonitoredCollection<T>
Extension Methods

Constructors

MonitoredCollection()

Creates a new monitored collection.

public MonitoredCollection()

MonitoredCollection(int)

Creates a new monitored collection with an upper limit to the number of elements.

public MonitoredCollection(int maxElements)

Parameters

maxElements int

The maximum number of elements; 0 for no limit.

Properties

MaxElements

The maximum number of elements; 0 for no limit.

public int MaxElements { get; }

Property Value

int

Methods

AddMany(IEnumerable<T>)

Adds all the items in collection to the collection that weren't already there.

public void AddMany(IEnumerable<T> collection)

Parameters

collection IEnumerable<T>

A collection of items to add to the collection.

Remarks

All events are raised en bloc after the items have been added.

After calling this method this collection will contain a superset of the items in collection, but not necessarily in the same order.

ClearItems()

Removes all elements from the System.Collections.ObjectModel.Collection<T>.

protected override void ClearItems()

InsertItem(int, T)

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

protected override void InsertItem(int index, T item)

Parameters

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

ArgumentOutOfRangeException

index is less than zero.-or-index is greater than System.Collections.ObjectModel.Collection<T>.Count.

RemoveItem(int)

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

protected override void RemoveItem(int index)

Parameters

index int

The zero-based index of the element to remove.

Exceptions

ArgumentOutOfRangeException

index is less than zero.-or-index is equal to or greater than System.Collections.ObjectModel.Collection<T>.Count.

SetItem(int, T)

Replaces the element at the specified index.

protected override void SetItem(int index, T item)

Parameters

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

ArgumentOutOfRangeException

index is less than zero.-or-index is greater than System.Collections.ObjectModel.Collection<T>.Count.

SetMany(IEnumerable<T>)

Adds all the items in enumeration to the collection that weren't already there and removes all items in the collection that are not in enumeration.

public void SetMany(IEnumerable<T> enumeration)

Parameters

enumeration IEnumerable<T>

An enumeration with items to add to the collection.

Remarks

All events are raised en bloc after the items have been added.

After calling this method this collection will contain the same items as enumeration, but not necessarily in the same order.

Events

Added

Occurs when a new item has just been added to the collection.

public event Action<T>? Added

Event Type

Action<T>

Changed

Occurs whenever something in the collection changes.

public event Action? Changed

Event Type

Action

Removed

Occurs when an item has just been removed from the collection.

public event Action<T>? Removed

Event Type

Action<T>

Removing

Occurs when an item is just about to be removed from the collection.

public event Action<T>? Removing

Event Type

Action<T>