コード例 #1
0
        public void Add()
        {
            IDictionary<string, string> dict = new Dictionary<string, string>();
            dict = dict.ToReadOnlyDictionary();

            Assert.Throws<NotSupportedException>(() => dict.Add("", ""));
        }
コード例 #2
0
        public void Remove()
        {
            IDictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("Key", "Value");

            dict = dict.ToReadOnlyDictionary();

            Assert.Throws<NotSupportedException>(() => dict.Remove("Key"));
        }
コード例 #3
0
        public void Keys()
        {
            IDictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("Key", "Value");
            dict.Add("OtherKey", "OtherValue");
            
            dict = dict.ToReadOnlyDictionary();

            Assert.IsTrue(dict.Keys.SequenceEqual(new string[] { "Key", "OtherKey" }));
        }
コード例 #4
0
        public void ContainsKey()
        {
            IDictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("Key", "Value");
            
            dict = dict.ToReadOnlyDictionary();

            Assert.IsTrue(dict.ContainsKey("Key"));
            Assert.IsFalse(dict.ContainsKey("OtherKey"));
        }
コード例 #5
0
        public void TryGetValue()
        {
            IDictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("Key", "Value");

            dict = dict.ToReadOnlyDictionary();

            string value = null;
            
            Assert.IsTrue(dict.TryGetValue("Key", out value));
            Assert.AreEqual("Value", value);

            Assert.IsFalse(dict.TryGetValue("OtherKey", out value));
            Assert.IsNull(value);
        }
コード例 #6
0
        public void GetEnumerator()
        {
            IDictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("Key", "Value");
            dict.Add("OtherKey", "OtherValue");

            dict = dict.ToReadOnlyDictionary();

            var values = new[]
            {
                KeyValuePair.Create("Key", "Value"),
                KeyValuePair.Create("OtherKey", "OtherValue")
            };

            IEnumerable enumerable = dict;
            Assert.IsTrue(enumerable.OfType<KeyValuePair<string, string>>().SequenceEqual(values));
        }
コード例 #7
0
        public void IsReadOnly()
        {
            IDictionary<string, string> dict = new Dictionary<string, string>();
            dict = dict.ToReadOnlyDictionary();

            Assert.IsTrue(dict.IsReadOnly);
        }
コード例 #8
0
        public void RemoveViaCollectionInterface()
        {
            IDictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("Key", "Value");

            dict = dict.ToReadOnlyDictionary();

            Assert.Throws<NotSupportedException>(() => dict.Remove(KeyValuePair.Create("Key", "Value")));
        }
コード例 #9
0
        public void Count()
        {
            IDictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("Key", "Value");
            dict.Add("OtherKey", "OtherValue");

            dict = dict.ToReadOnlyDictionary();

            Assert.AreEqual(2, dict.Count);
        }
コード例 #10
0
        public void CopyTo()
        {
            IDictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("Key", "Value");
            dict.Add("OtherKey", "OtherValue");

            dict = dict.ToReadOnlyDictionary();

            var values = new KeyValuePair<string, string>[3];

            var expectedValues = new[]
            {
                KeyValuePair.Create("Key", "Value"),
                KeyValuePair.Create("OtherKey", "OtherValue"),
                default(KeyValuePair<string, string>)
            };

            dict.CopyTo(values, 0);
            Assert.IsTrue(values.SequenceEqual(expectedValues));

            dict.CopyTo(values, 1);

            expectedValues[2] = expectedValues[1];
            expectedValues[1] = expectedValues[0];

            Assert.IsTrue(values.SequenceEqual(expectedValues));
        }
コード例 #11
0
        public void Contains()
        {
            IDictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("Key", "Value");
            dict.Add("OtherKey", "OtherValue");

            dict = dict.ToReadOnlyDictionary();

            Assert.IsTrue(dict.Contains(KeyValuePair.Create("Key", "Value")));
            Assert.IsTrue(dict.Contains(KeyValuePair.Create("OtherKey", "OtherValue")));

            Assert.IsFalse(dict.Contains(KeyValuePair.Create("NonExistent", "NonExistentValue")));
        }
コード例 #12
0
        public void SetViaKeyIndexer()
        {
            IDictionary<string, string> dict = new Dictionary<string, string>();
            dict = dict.ToReadOnlyDictionary();

            Assert.Throws<NotSupportedException>(() => dict["Key"] = "Value");
        }
コード例 #13
0
        public void GetViaKeyIndexer()
        {
            IDictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("Key", "Value");
            dict.Add("OtherKey", "OtherValue");

            dict = dict.ToReadOnlyDictionary();

            Assert.AreEqual("Value", dict["Key"]);
            Assert.AreEqual("OtherValue", dict["OtherKey"]);

            Assert.Throws<KeyNotFoundException>(() => { string val = dict["NonExistentValue"]; });
        }