Exemple #1
0
        static ImmutableHashDictionary()
        {
            // Need to make sure _emptyDictionary gets initialized first, otherwise the constructor for Empty will NRE.
            _emptyDictionary = new Dictionary <TKey, TValue>();

            Empty = new ImmutableHashDictionary <TKey, TValue>();
        }
            /// <summary>
            /// Converts the current contents of the builder to a new <see cref="ImmutableHashDictionary{TKey, TValue}"/>.
            /// </summary>
            /// <returns>An <see cref="ImmutableHashDictionary{TKey, TValue}"/>, containing the same entries as the buidler instance.</returns>
            /// <remarks>
            /// This method is an O(1) operation, as it simply involves wrapping a new <see cref="ImmutableHashDictionary{TKey, TValue}"/>
            /// around the <see cref="Dictionary{TKey, TValue}"/> that has been built. The next mutation operation performed on this builder
            /// will be at least O(N), as it will require duplicating the entire <see cref="Dictionary{TKey, TValue}"/>, to prevent the
            /// previously-created <see cref="ImmutableHashDictionary{TKey, TValue}"/> from being mutated.
            /// </remarks>
            public ImmutableHashDictionary <TKey, TValue> ToImmutable()
            {
                var immutableHashDictionary = new ImmutableHashDictionary <TKey, TValue>(CurrentDictionaryForRead, _keyComparer, _valueComparer);

                _lastDictionary    = _currentDictionary;
                _currentDictionary = null;

                return(immutableHashDictionary);
            }