Exemple #1
0
        public void ReadOnlyDictionaryAsReadOnlyDictionary()
        {
            // AsReadOnlyDictionary should not rewrap a ReadOnlyCollection
            var dictionary = new Dictionary <int, int> {
                [1] = 1, [2] = 4
            };
            IEnumerable <KeyValuePair <int, int> > readOnlyDictionary = new ReadOnlyDictionary <int, int>(dictionary);

            Assert.AreSame(readOnlyDictionary, readOnlyDictionary.AsReadOnlyDictionary());
        }
    public void DictionaryAdapterAvoidsWrapping() {
        var d = 5.Range().ToDictionary(i => i, i => i*i);
        
        // existing readonly dictionary class is not wrapped
        var rd = new ReadOnlyDictionary<int, int>(d);
        rd.AsReadOnlyDictionary().AssertReferenceEquals(rd);
        rd.AsIDictionary().AssertReferenceEquals(rd);

        // back-and-forth of an IReadOnlyDictionary has no effect
        var ar = d.AsReadOnlyDictionary();
        ar.AsIDictionary().AssertReferenceEquals(ar);
        ReadOnlyDic.AsIDictionary().AsReadOnlyDictionary().AssertReferenceEquals(ReadOnlyDic);
    }
    public void DictionaryAdapterAvoidsWrapping()
    {
        var d = 5.Range().ToDictionary(i => i, i => i * i);

        // existing readonly dictionary class is not wrapped
        var rd = new ReadOnlyDictionary <int, int>(d);

        rd.AsReadOnlyDictionary().AssertReferenceEquals(rd);
        rd.AsIDictionary().AssertReferenceEquals(rd);

        // back-and-forth of an IReadOnlyDictionary has no effect
        var ar = d.AsReadOnlyDictionary();

        ar.AsIDictionary().AssertReferenceEquals(ar);
        ReadOnlyDic.AsIDictionary().AsReadOnlyDictionary().AssertReferenceEquals(ReadOnlyDic);
    }
Exemple #4
0
 /// <summary>
 /// Gets the value associated with a specified key as an option.
 /// </summary>
 /// <typeparam name="TKey">The type of the keys in <paramref name="source"/>.</typeparam>
 /// <typeparam name="TValue">The type of the values in <paramref name="source"/>.</typeparam>
 /// <param name="source">A source dictionary.</param>
 /// <param name="key">The key whose value to get.</param>
 /// <returns>
 /// An option containing the value associated with <paramref name="key"/> in <paramref
 /// name="source"/>, if it is found; otherwise, an empty option.
 /// </returns>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="source"/> or <paramref name="key"/> is <see langword="null"/>.
 /// </exception>
 public static Opt <TValue> GetValueOpt <TKey, TValue>(this ReadOnlyDictionary <TKey, TValue> source, TKey key) =>
 source.AsReadOnlyDictionary().GetValueOpt(key);