ChangeTrackingDictionary<TKey, TValue>
class ChangeTrackingDictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable, IReadOnlyDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Azure.Storage.Common
{
internal class ChangeTrackingDictionary<[System.Runtime.CompilerServices.Nullable(1)] TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable, IReadOnlyDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>>
{
private IDictionary<TKey, TValue> _innerDictionary;
public bool IsUndefined => _innerDictionary == null;
public int Count {
get {
if (!IsUndefined)
return EnsureDictionary().Count;
return 0;
}
}
public bool IsReadOnly {
get {
if (!IsUndefined)
return EnsureDictionary().IsReadOnly;
return false;
}
}
public ICollection<TKey> Keys {
get {
if (!IsUndefined)
return EnsureDictionary().Keys;
return Array.Empty<TKey>();
}
}
public ICollection<TValue> Values {
get {
if (!IsUndefined)
return EnsureDictionary().Values;
return Array.Empty<TValue>();
}
}
public TValue this[TKey key] {
get {
if (IsUndefined)
throw new KeyNotFoundException("key");
return EnsureDictionary()[key];
}
set {
EnsureDictionary()[key] = value;
}
}
IEnumerable<TKey> IReadOnlyDictionary<TKey, TValue>.Keys {
get {
return Keys;
}
}
IEnumerable<TValue> IReadOnlyDictionary<TKey, TValue>.Values {
get {
return Values;
}
}
public ChangeTrackingDictionary()
{
}
public ChangeTrackingDictionary(IDictionary<TKey, TValue> dictionary)
{
if (dictionary != null)
_innerDictionary = new Dictionary<TKey, TValue>(dictionary);
}
public ChangeTrackingDictionary(IReadOnlyDictionary<TKey, TValue> dictionary)
{
if (dictionary != null) {
_innerDictionary = new Dictionary<TKey, TValue>();
foreach (KeyValuePair<TKey, TValue> item in dictionary) {
_innerDictionary.Add(item);
}
}
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
if (IsUndefined)
return <GetEnumerator>g__enumerateEmpty|21_0();
return EnsureDictionary().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(KeyValuePair<TKey, TValue> item)
{
EnsureDictionary().Add(item);
}
public void Clear()
{
EnsureDictionary().Clear();
}
public bool Contains(KeyValuePair<TKey, TValue> item)
{
if (IsUndefined)
return false;
return EnsureDictionary().Contains(item);
}
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int index)
{
if (!IsUndefined)
EnsureDictionary().CopyTo(array, index);
}
public bool Remove(KeyValuePair<TKey, TValue> item)
{
if (IsUndefined)
return false;
return EnsureDictionary().Remove(item);
}
public void Add(TKey key, TValue value)
{
EnsureDictionary().Add(key, value);
}
public bool ContainsKey(TKey key)
{
if (IsUndefined)
return false;
return EnsureDictionary().ContainsKey(key);
}
public bool Remove(TKey key)
{
if (IsUndefined)
return false;
return EnsureDictionary().Remove(key);
}
public bool TryGetValue(TKey key, out TValue value)
{
if (IsUndefined) {
value = default(TValue);
return false;
}
return EnsureDictionary().TryGetValue(key, out value);
}
public IDictionary<TKey, TValue> EnsureDictionary()
{
return _innerDictionary ?? (_innerDictionary = new Dictionary<TKey, TValue>());
}
}
}