ObservableDictionary<TKey, TValue> 类
- 程序集
- ClassIsland.Shared.dll
同时实现 IDictionary、IList、INotifyCollectionChanged 的字典结构。
public class ObservableDictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IReadOnlyDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, INotifyCollectionChanged, INotifyPropertyChanged, IDictionary, ICollection, IEnumerable where TKey : notnull
类型参数
TKey字典键类型
TValue字典值类型
- 继承
-
ObservableDictionary<TKey, TValue>
- 实现
-
IDictionary<TKey, TValue>ICollection<KeyValuePair<TKey, TValue>>IReadOnlyDictionary<TKey, TValue>IReadOnlyCollection<KeyValuePair<TKey, TValue>>IEnumerable<KeyValuePair<TKey, TValue>>
- 继承成员
- 扩展方法
构造函数
ObservableDictionary()
Initializes a new instance of the AvaloniaDictionary<TKey, TValue> class.
public ObservableDictionary()
ObservableDictionary(IDictionary<TKey, TValue>, IEqualityComparer<TKey>?)
Initializes a new instance of the AvaloniaDictionary<TKey, TValue> class using an IDictionary.
public ObservableDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey>? comparer = null)
参数
dictionaryIDictionary<TKey, TValue>comparerIEqualityComparer<TKey>
ObservableDictionary(int)
Initializes a new instance of the AvaloniaDictionary<TKey, TValue> class.
public ObservableDictionary(int capacity)
参数
capacityint
属性
Count
Gets the number of elements contained in the ICollection<T>.
public int Count { get; }
属性值
- int
The number of elements contained in the ICollection<T>.
IsReadOnly
Gets a value indicating whether the ICollection<T> is read-only.
public bool IsReadOnly { get; }
属性值
- bool
true if the ICollection<T> is read-only; otherwise, false.
this[TKey]
Gets or sets the named resource.
public TValue this[TKey key] { get; set; }
参数
keyTKeyThe resource key.
属性值
- TValue
The resource, or null if not found.
Keys
Gets an ICollection<T> containing the keys of the IDictionary<TKey, TValue>.
public ICollection<TKey> Keys { get; }
属性值
- ICollection<TKey>
An ICollection<T> containing the keys of the object that implements IDictionary<TKey, TValue>.
Values
Gets an ICollection<T> containing the values in the IDictionary<TKey, TValue>.
public ICollection<TValue> Values { get; }
属性值
- ICollection<TValue>
An ICollection<T> containing the values in the object that implements IDictionary<TKey, TValue>.
方法
Add(TKey, TValue)
Adds an element with the provided key and value to the IDictionary<TKey, TValue>.
public void Add(TKey key, TValue value)
参数
keyTKeyThe object to use as the key of the element to add.
valueTValueThe object to use as the value of the element to add.
异常
- ArgumentNullException
keyis null.- ArgumentException
An element with the same key already exists in the IDictionary<TKey, TValue>.
- NotSupportedException
The IDictionary<TKey, TValue> is read-only.
Clear()
Removes all items from the ICollection<T>.
public void Clear()
异常
- NotSupportedException
The ICollection<T> is read-only.
ContainsKey(TKey)
Determines whether the IDictionary<TKey, TValue> contains an element with the specified key.
public bool ContainsKey(TKey key)
参数
keyTKeyThe key to locate in the IDictionary<TKey, TValue>.
返回
- bool
true if the IDictionary<TKey, TValue> contains an element with the key; otherwise, false.
异常
- ArgumentNullException
keyis null.
CopyTo(KeyValuePair<TKey, TValue>[], int)
Copies the elements of the ICollection<T> to an Array, starting at a particular Array index.
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
参数
arrayKeyValuePair<TKey, TValue>[]The one-dimensional Array that is the destination of the elements copied from ICollection<T>. The Array must have zero-based indexing.
arrayIndexintThe zero-based index in
arrayat which copying begins.
异常
- ArgumentNullException
arrayis null.- ArgumentOutOfRangeException
arrayIndexis less than 0.- ArgumentException
The number of elements in the source ICollection<T> is greater than the available space from
arrayIndexto the end of the destinationarray.
GetEnumerator()
Returns an enumerator that iterates through the collection.
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
返回
- IEnumerator<KeyValuePair<TKey, TValue>>
An enumerator that can be used to iterate through the collection.
Remove(TKey)
Removes the element with the specified key from the IDictionary<TKey, TValue>.
public bool Remove(TKey key)
参数
keyTKeyThe key of the element to remove.
返回
- bool
true if the element is successfully removed; otherwise, false. This method also returns false if
keywas not found in the original IDictionary<TKey, TValue>.
异常
- ArgumentNullException
keyis null.- NotSupportedException
The IDictionary<TKey, TValue> is read-only.
TryGetValue(TKey, out TValue)
Gets the value associated with the specified key.
public bool TryGetValue(TKey key, out TValue value)
参数
keyTKeyThe key whose value to get.
valueTValueWhen this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the
valueparameter. This parameter is passed uninitialized.
返回
- bool
true if the object that implements IDictionary<TKey, TValue> contains an element with the specified key; otherwise, false.
异常
- ArgumentNullException
keyis null.
事件
CollectionChanged
Occurs when the collection changes.
public event NotifyCollectionChangedEventHandler? CollectionChanged
事件类型
PropertyChanged
Raised when a property on the collection changes.
public event PropertyChangedEventHandler? PropertyChanged