public void TestNullableKeyDictionaryContainsKey()
        {
            var dict = new NullableKeyDictionary <string, string>(StringComparer.OrdinalIgnoreCase)
            {
                { "Hello", "World" },
                { null, "Null" }
            };

            Assert.IsTrue(dict["hello"] == dict["Hello"]);
            Assert.IsTrue(dict.ContainsKey("hello"));
            Assert.IsTrue(dict.ContainsKey(null));
        }
示例#2
0
        /// <summary>
        /// Determines whether a specified key is in the System.Linq.Lookup&lt;TKey,TElement&gt;.
        /// </summary>
        /// <param name="key">The key to find in the System.Linq.Lookup&lt;TKey,TElement&gt;.</param>
        /// <returns>true if key is in the System.Linq.Lookup&lt;TKey,TElement&gt;; otherwise, false.</returns>
        public bool Contains(TKey key)
        {
            //HACK # Lookup.Contains() hack
            // # Work around for doomed (not optimized) System.Linq.Lookup implementation calling GetHashCode() for given key even it holds no elements.
            // # Underlying dictionary is optimized and DOES NOT call GetHashCode() for given key if it is EMPTY because it holds NO ELEMENTS TO COMPARE afterwards.
            // # Calling for GetHashCode() manually for the sake of System.Linq.Lookup behavior.
            if (mKeyGroupPairs.Count == 0)
            {
                mKeyGroupPairs.Comparer.GetHashCode(key);
            }

            return(mKeyGroupPairs.ContainsKey(key));
        }
        public void NullableKeyDictionaryTest()
        {
            int value = 0;
            IDictionary <string, int> target = new NullableKeyDictionary <string, int>();

            target.Add(null, 13);
            target.Add(new KeyValuePair <string, int>("a", 14));
            Assert.IsTrue(target.ContainsKey(null));
            Assert.IsTrue(target.ContainsKey("a"));
            Assert.IsTrue(target.Contains(new KeyValuePair <string, int>(null, 13)));
            Assert.IsTrue(target.Contains(new KeyValuePair <string, int>("a", 14)));
            Assert.IsTrue(target.Keys.Contains(null));
            Assert.IsTrue(target.Keys.Contains("a"));
            Assert.IsTrue(target.Values.Contains(13));
            Assert.IsTrue(target.Values.Contains(14));
            Assert.AreEqual(target.Count, 2);

            Assert.IsTrue(target.TryGetValue(null, out value));
            Assert.AreEqual(value, 13);
            Assert.IsTrue(target.TryGetValue("a", out value));
            Assert.AreEqual(value, 14);

            foreach (KeyValuePair <string, int> item in target)
            {
                Console.WriteLine(item);
            }

            Assert.IsTrue(target.Remove(null));
            Assert.IsFalse(target.Remove(new KeyValuePair <string, int>(null, 13)));
            Assert.IsTrue(target.Remove("a"));
            Assert.IsFalse(target.Remove(new KeyValuePair <string, int>("a", 14)));

            foreach (KeyValuePair <string, int> item in target)
            {
                Console.WriteLine(item);
            }
        }