本文整理汇总了C#中ReadOnlyDictionary.CopyTo方法的典型用法代码示例。如果您正苦于以下问题:C# ReadOnlyDictionary.CopyTo方法的具体用法?C# ReadOnlyDictionary.CopyTo怎么用?C# ReadOnlyDictionary.CopyTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ReadOnlyDictionary
的用法示例。
在下文中一共展示了ReadOnlyDictionary.CopyTo方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadOnlyDictionary_Unit_CopyTo_Optimal
public void ReadOnlyDictionary_Unit_CopyTo_Optimal()
{
IDictionary<String, String> dictionary = new Dictionary<String, String>() {
{ "Key1", "Value1" },
{ "Key2", "Value2" },
{ "Key3", "Value3" }
};
ReadOnlyDictionary<String, String> target = new ReadOnlyDictionary<String, String>(dictionary);
KeyValuePair<String, String>[] array = new KeyValuePair<String, String>[dictionary.Count];
Int32 arrayIndex = 0;
target.CopyTo(array, arrayIndex);
Assert.AreEqual(dictionary.Count, array.Length);
foreach (KeyValuePair<String, String> pair in array) {
Assert.IsTrue(dictionary.Contains(pair));
}
}
示例2: TestReadOperations
public void TestReadOperations()
{
var dic = new ReadOnlyDictionary<string, string>(new Dictionary<string, string>() {
{"key1", "val1"},
{"key2", "val2"},
{"key3", "val3"},
});
Assert.AreEqual(3, dic.Count);
Assert.AreEqual("val1", dic["key1"]);
Assert.IsTrue(dic.Contains(new KeyValuePair<string, string>("key2", "val2")));
Assert.IsTrue(dic.ContainsKey("key3"));
foreach (var pair in dic) {
}
foreach (var key in dic.Keys) {
}
foreach (var val in dic.Values) {
}
var pairs = new KeyValuePair<string, string>[3];
dic.CopyTo(pairs, 0);
string outval = null;
Assert.IsTrue(dic.TryGetValue("key1", out outval));
Assert.AreEqual(outval, "val1");
}
示例3: ReadOnlyDictionary_Unit_CopyTo_ArrayIsTooSmall
public void ReadOnlyDictionary_Unit_CopyTo_ArrayIsTooSmall()
{
IDictionary<String, String> dictionary = new Dictionary<String, String>() {
{ "Key1", "Value1" },
{ "Key2", "Value2" },
{ "Key3", "Value3" }
};
ReadOnlyDictionary<String, String> target = new ReadOnlyDictionary<String, String>(dictionary);
KeyValuePair<String, String>[] array = new KeyValuePair<String, String>[dictionary.Count - 2];
Int32 arrayIndex = 1;
target.CopyTo(array, arrayIndex);
}