Table of Contents

ObservableDictionary<TKey, TValue> 类

命名空间
ClassIsland.Shared.ComponentModels
程序集
ClassIsland.Shared.dll

同时实现 IDictionaryIListINotifyCollectionChanged 的字典结构。

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>
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)

参数

dictionary IDictionary<TKey, TValue>
comparer IEqualityComparer<TKey>

ObservableDictionary(int)

Initializes a new instance of the AvaloniaDictionary<TKey, TValue> class.

public ObservableDictionary(int capacity)

参数

capacity int

属性

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; }

参数

key TKey

The 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)

参数

key TKey

The object to use as the key of the element to add.

value TValue

The object to use as the value of the element to add.

异常

ArgumentNullException

key is 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)

参数

key TKey

The key to locate in the IDictionary<TKey, TValue>.

返回

bool

true if the IDictionary<TKey, TValue> contains an element with the key; otherwise, false.

异常

ArgumentNullException

key is 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)

参数

array KeyValuePair<TKey, TValue>[]

The one-dimensional Array that is the destination of the elements copied from ICollection<T>. The Array must have zero-based indexing.

arrayIndex int

The zero-based index in array at which copying begins.

异常

ArgumentNullException

array is null.

ArgumentOutOfRangeException

arrayIndex is less than 0.

ArgumentException

The number of elements in the source ICollection<T> is greater than the available space from arrayIndex to the end of the destination array.

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)

参数

key TKey

The key of the element to remove.

返回

bool

true if the element is successfully removed; otherwise, false. This method also returns false if key was not found in the original IDictionary<TKey, TValue>.

异常

ArgumentNullException

key is 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)

参数

key TKey

The key whose value to get.

value TValue

When this method returns, 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.

返回

bool

true if the object that implements IDictionary<TKey, TValue> contains an element with the specified key; otherwise, false.

异常

ArgumentNullException

key is null.

事件

CollectionChanged

Occurs when the collection changes.

public event NotifyCollectionChangedEventHandler? CollectionChanged

事件类型

NotifyCollectionChangedEventHandler

PropertyChanged

Raised when a property on the collection changes.

public event PropertyChangedEventHandler? PropertyChanged

事件类型

PropertyChangedEventHandler