public NullAwareDictionary(KeyValuePair <TKey, TValue>[] dictionary)
        {
            if (dictionary == null)
            {
                throw new ArgumentNullException(nameof(dictionary), "dictionary is null.");
            }

            _hasNull       = false;
            _valueForNull  = new[] { default(TValue) };
            _valueComparer = EqualityComparer <TValue> .Default;
            if (typeof(TKey).CanBeNull())
            {
                Keys   = new ConditionalExtendedList <TKey>(new[] { default(TKey) }, _wrapped.Keys, () => _hasNull, null);
                Values = new ConditionalExtendedList <TValue>(_valueForNull, _wrapped.Values, () => _hasNull, null);
            }
            else
            {
                Keys   = new ProxyCollection <TKey>(() => _wrapped.Keys).AsReadOnlyICollection;
                Values = new ProxyCollection <TValue>(() => _wrapped.Values).AsReadOnlyICollection;
            }

            AsReadOnly = new ReadOnlyDictionary <TKey, TValue>(this);
            foreach (var pair in dictionary)
            {
                Add(pair.Key, pair.Value);
            }
        }
Example #2
0
 public NullAwareDictionary(IEqualityComparer <TKey> comparer)
 {
     _wrapped       = new Dictionary <TKey, TValue>(comparer);
     _hasNull       = false;
     _valueForNull  = new[] { default(TValue) };
     _valueComparer = EqualityComparer <TValue> .Default;
     if (typeof(TKey).CanBeNull())
     {
         Keys   = new ConditionalExtendedList <TKey>(new[] { default(TKey) }, _wrapped.Keys, () => _hasNull, null);
         Values = new ConditionalExtendedList <TValue>(_valueForNull, _wrapped.Values, () => _hasNull, null);
     }
     else
     {
         Keys   = new ProxyCollection <TKey>(() => _wrapped.Keys).AsReadOnlyICollection;
         Values = new ProxyCollection <TValue>(() => _wrapped.Values).AsReadOnlyICollection;
     }
     AsReadOnly = new ReadOnlyDictionary <TKey, TValue>(this);
 }