Class KeyItemCollection<T>
- Namespace
- Loehnert.Utility.Collections
- Assembly
- Loehnert.Utility.dll
Represents an observable collection for items that implement the IHasKey interface.
public class KeyItemCollection<T> : IKeyItemCollection<T>, IList<T>, ICollection<T>, IEnumerable<T>, INotifyCollectionChanged, IList, ICollection, IEnumerable, INotifyPropertyChanged where T : IHasKey
Type Parameters
T
The type of the elements in the collection.
- Inheritance
-
KeyItemCollection<T>
- Implements
-
IList<T>ICollection<T>IEnumerable<T>
- Derived
- Inherited Members
- Extension Methods
Constructors
KeyItemCollection(IEqualityComparer)
Initializes a new instance of the KeyItemCollection<T> class.
public KeyItemCollection(IEqualityComparer comparer = null)
Parameters
comparer
IEqualityComparerA comparer to determine whether two keys are equal or null to use the default comparer.
KeyItemCollection(string, IEnumerable<T>, IEqualityComparer)
Initializes a new instance of the KeyItemCollection<T> class.
public KeyItemCollection(string collectionItemDescription, IEnumerable<T> items, IEqualityComparer comparer = null)
Parameters
collectionItemDescription
stringA short phrase for a collection item, for example 'User' for user collection.
items
IEnumerable<T>These items will be added to the collection.
comparer
IEqualityComparerA comparer to determine whether two keys are equal or null to use the default comparer.
Exceptions
- ArgumentNullException
Thrown when
collectionItemDescription
is null or empty oritems
is null.
KeyItemCollection(string, IEqualityComparer)
Initializes a new instance of the KeyItemCollection<T> class.
public KeyItemCollection(string collectionItemDescription, IEqualityComparer comparer = null)
Parameters
collectionItemDescription
stringA short phrase for a collection item, for example 'User' for user collection.
comparer
IEqualityComparerA comparer to determine whether two keys are equal or null to use the default comparer.
Exceptions
- ArgumentNullException
Thrown when
collectionItemDescription
is null or empty.
Fields
_items
Gets the ordered items.
protected OrderedDictionary _items
Field Value
_syncRoot
Gets the sync object, used for locking.
protected object _syncRoot
Field Value
Properties
Count
Gets the number of elements contained in the collection.
public int Count { get; }
Property Value
IsReadOnly
Gets a value indicating whether the collection is read-only.
public bool IsReadOnly { get; }
Property Value
IsSynchronized
Gets a value indicating whether access to the collection is synchronized (thread safe).
public bool IsSynchronized { get; }
Property Value
this[int]
Gets or sets the element at the specified index.
public T this[int index] { get; set; }
Parameters
index
intThe zero-based index of the element to get or set.
Property Value
- T
The element at the specified index.
Exceptions
- ArgumentOutOfRangeException
Thrown when
index
is less than null, or greater or equal than the Count property.
this[string]
Gets the item associated with the specified key.
public T this[string key] { get; }
Parameters
key
stringThe key of the seeked item.
Property Value
- T
The item associated with the specified key.
Exceptions
- ArgumentNullException
Thrown when
key
is null, empty or whitespace.- KeyNotFoundException
Thrown when an item with the specified
key
is not found.
Keys
Gets the keys of the items.
public IEnumerable<string> Keys { get; }
Property Value
SyncRoot
Gets a synchronization lock object.
public object SyncRoot { get; }
Property Value
Methods
Add(T)
Adds an item to the end of the collection.
public virtual void Add(T item)
Parameters
item
TThe item to be added.
Exceptions
- ArgumentNullException
Thrown when
item.key.key
is null, empty or whitespace, oritem
is null.- ArgumentException
Thrown when the collection already contains an item with the same key as
item
.
AddRange(IEnumerable<T>)
Adds a range of items to the end of the collection.
public virtual void AddRange(IEnumerable<T> items)
Parameters
items
IEnumerable<T>The items to be added.
Exceptions
- ArgumentNullException
Thrown when the key of a item is null, empty or whitespace, or
items
is null.- ArgumentException
Thrown when the collection already contains an item with the same key as one of the
items
.
Clear()
Removes all elements.
public void Clear()
Contains(T)
Determines whether the collection contains an item with the same key as item
.
public virtual bool Contains(T item)
Parameters
item
TThe object to locate in the Collection<T>.
Returns
- bool
True if item is found.
ContainsKey(string)
Determines whether the collection contains an item with the key
.
public bool ContainsKey(string key)
Parameters
key
stringThe key to locate.
Returns
- bool
True if item is found.
CopyTo(T[], int)
Copies the entire collection to a compatible one-dimensional array, starting at the specified index of the target array.
public void CopyTo(T[] array, int arrayIndex)
Parameters
array
T[]The one-dimensional array that is the destination of the copied elements. The Array must have zero-based indexing.
arrayIndex
intThe zero-based index in array at which copying begins.
GetEnumerator()
Returns an enumerator.
public IEnumerator<T> GetEnumerator()
Returns
- IEnumerator<T>
An IEnumerator<T> for the collection.
IndexOf(T)
Determines the index of a specific item in the collection.
public int IndexOf(T item)
Parameters
item
TThe object to locate in the collection.
Returns
- int
The index of item if found in the collection, otherwise -1.
Insert(int, T)
Inserts an item into the collection at the specified index.
public void Insert(int index, T item)
Parameters
index
intThe zero-based index at which item should be inserted.
item
TThe object to be inserted.
Exceptions
- ArgumentOutOfRangeException
Thrown when
index
is out of range.
Remove(T)
Removes the item where the Key equals with the item key from the collection./>.
public virtual bool Remove(T item)
Parameters
item
TThe item to remove.
Returns
- bool
True, if it could be removed.
Exceptions
- ArgumentNullException
Thrown when
item
is null.
RemoveAt(int)
Removes the item at the specified index.
public void RemoveAt(int index)
Parameters
index
intThe zero-based index of the item to remove.
Exceptions
- ArgumentOutOfRangeException
Thrown when
index
is out of range.
TryGetValue(string, out T)
Gets the value associated with the specified key.
public bool TryGetValue(string key, out T value)
Parameters
key
stringThe key of the value to get.
value
TWhen this method returns, contains the value associated with the specified key, if the key is found; otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized.
Returns
- bool
true
if the KeyItemCollection<T> contains an element with the specified key; otherwisefalse
.
Exceptions
- ArgumentNullException
Thrown when
key
is null, empty or whitespace.
Events
CollectionChanged
Occurs after the collection has changed.
public event NotifyCollectionChangedEventHandler CollectionChanged
Event Type
PropertyChanged
Occurs when a property of an item in the collection changes.
public event PropertyChangedEventHandler PropertyChanged