本文整理汇总了C#中System.Collections.Dictionary.ToReadOnlyDictionary方法的典型用法代码示例。如果您正苦于以下问题:C# Dictionary.ToReadOnlyDictionary方法的具体用法?C# Dictionary.ToReadOnlyDictionary怎么用?C# Dictionary.ToReadOnlyDictionary使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.Dictionary
的用法示例。
在下文中一共展示了Dictionary.ToReadOnlyDictionary方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Add
public void Add()
{
IDictionary<string, string> dict = new Dictionary<string, string>();
dict = dict.ToReadOnlyDictionary();
Assert.Throws<NotSupportedException>(() => dict.Add("", ""));
}
示例2: Remove
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: Keys
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: ContainsKey
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: TryGetValue
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: GetEnumerator
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: IsReadOnly
public void IsReadOnly()
{
IDictionary<string, string> dict = new Dictionary<string, string>();
dict = dict.ToReadOnlyDictionary();
Assert.IsTrue(dict.IsReadOnly);
}
示例8: RemoveViaCollectionInterface
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: Count
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: CopyTo
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: Contains
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: SetViaKeyIndexer
public void SetViaKeyIndexer()
{
IDictionary<string, string> dict = new Dictionary<string, string>();
dict = dict.ToReadOnlyDictionary();
Assert.Throws<NotSupportedException>(() => dict["Key"] = "Value");
}
示例13: GetViaKeyIndexer
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"]; });
}