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
TThe 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
comparerIEqualityComparerA 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
collectionItemDescriptionstringA short phrase for a collection item, for example 'User' for user collection.
itemsIEnumerable<T>These items will be added to the collection.
comparerIEqualityComparerA comparer to determine whether two keys are equal or null to use the default comparer.
Exceptions
- ArgumentNullException
Thrown when
collectionItemDescriptionis null or empty oritemsis null.
KeyItemCollection(string, IEqualityComparer)
Initializes a new instance of the KeyItemCollection<T> class.
public KeyItemCollection(string collectionItemDescription, IEqualityComparer comparer = null)
Parameters
collectionItemDescriptionstringA short phrase for a collection item, for example 'User' for user collection.
comparerIEqualityComparerA comparer to determine whether two keys are equal or null to use the default comparer.
Exceptions
- ArgumentNullException
Thrown when
collectionItemDescriptionis 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
indexintThe zero-based index of the element to get or set.
Property Value
- T
The element at the specified index.
Exceptions
- ArgumentOutOfRangeException
Thrown when
indexis 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
keystringThe key of the seeked item.
Property Value
- T
The item associated with the specified key.
Exceptions
- ArgumentNullException
Thrown when
keyis null, empty or whitespace.- KeyNotFoundException
Thrown when an item with the specified
keyis 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
itemTThe item to be added.
Exceptions
- ArgumentNullException
Thrown when
item.key.keyis null, empty or whitespace, oritemis 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
itemsIEnumerable<T>The items to be added.
Exceptions
- ArgumentNullException
Thrown when the key of a item is null, empty or whitespace, or
itemsis 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
itemTThe 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
keystringThe 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
arrayT[]The one-dimensional array that is the destination of the copied elements. The Array must have zero-based indexing.
arrayIndexintThe 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
itemTThe 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
indexintThe zero-based index at which item should be inserted.
itemTThe object to be inserted.
Exceptions
- ArgumentOutOfRangeException
Thrown when
indexis 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
itemTThe item to remove.
Returns
- bool
True, if it could be removed.
Exceptions
- ArgumentNullException
Thrown when
itemis null.
RemoveAt(int)
Removes the item at the specified index.
public void RemoveAt(int index)
Parameters
indexintThe zero-based index of the item to remove.
Exceptions
- ArgumentOutOfRangeException
Thrown when
indexis out of range.
TryGetValue(string, out T)
Gets the value associated with the specified key.
public bool TryGetValue(string key, out T value)
Parameters
keystringThe key of the value to get.
valueTWhen 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
trueif the KeyItemCollection<T> contains an element with the specified key; otherwisefalse.
Exceptions
- ArgumentNullException
Thrown when
keyis 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