本文整理汇总了C#中System.Collections.SortedList.CopyTo方法的典型用法代码示例。如果您正苦于以下问题:C# SortedList.CopyTo方法的具体用法?C# SortedList.CopyTo怎么用?C# SortedList.CopyTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.SortedList
的用法示例。
在下文中一共展示了SortedList.CopyTo方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestCopyTo
public void TestCopyTo ()
{
SortedList sl1 = new SortedList ();
for (int i = 0; i <= 10; i++) { sl1.Add ("kala " + i, i); }
{
try {
sl1.CopyTo (null, 2);
Assert.Fail ("sl.CopyTo: does not throw ArgumentNullException when target null");
} catch (ArgumentNullException) {
}
}
{
try {
Char [,] c2 = new Char [2, 2];
sl1.CopyTo (c2, 2);
Assert.Fail ("sl.CopyTo: does not throw ArgumentException when target is multiarray");
} catch (ArgumentException) {
}
}
{
try {
Char [] c1 = new Char [2];
sl1.CopyTo (c1, -2);
Assert.Fail ("sl.CopyTo: does not throw ArgumentOutOfRangeException when index is negative");
} catch (ArgumentOutOfRangeException) {
}
}
{
try {
Char [] c1 = new Char [2];
sl1.CopyTo (c1, 3);
Assert.Fail ("sl.CopyTo: does not throw ArgumentException when index is too large");
} catch (ArgumentException) {
}
}
{
try {
Char [] c1 = new Char [2];
sl1.CopyTo (c1, 1);
Assert.Fail ("sl.CopyTo: does not throw ArgumentException when SortedList too big for the array");
} catch (ArgumentException) {
}
}
{
try {
Char [] c2 = new Char [15];
sl1.CopyTo (c2, 0);
Assert.Fail ("sl.CopyTo: does not throw InvalidCastException when incompatible data types");
} catch (InvalidCastException) {
}
}
// CopyTo function does not work well with SortedList
// even example at MSDN gave InvalidCastException
// thus, it is NOT tested here
/*
sl1.Clear();
for (int i = 0; i <= 5; i++) {sl1.Add(i,""+i);}
Char[] copy = new Char[15];
Array.Clear(copy,0,copy.Length);
copy.SetValue( "The", 0 );
copy.SetValue( "quick", 1 );
copy.SetValue( "brown", 2 );
copy.SetValue( "fox", 3 );
copy.SetValue( "jumped", 4 );
copy.SetValue( "over", 5 );
copy.SetValue( "the", 6 );
copy.SetValue( "lazy", 7 );
copy.SetValue( "dog", 8 );
sl1.CopyTo(copy,1);
AssertEquals("sl.CopyTo: incorrect copy(1).","The", copy.GetValue(0));
AssertEquals("sl.CopyTo: incorrect copy(1).","quick", copy.GetValue(1));
for (int i=2; i<8; i++) AssertEquals("sl.CopyTo: incorrect copy(2).",sl1["kala "+(i-2)], copy.GetValue(i));
AssertEquals("sl.CopyTo: incorrect copy(3).","dog", copy.GetValue(8));
*/
}
示例2: DoICollectionTests
private void DoICollectionTests(SortedList good, ICollection bad, Hashtable hsh1, DicType dic)
{
if (good.Count != bad.Count)
hsh1["Count"] = "";
if (good.IsSynchronized != bad.IsSynchronized)
hsh1["IsSynchronized"] = "";
if (good.SyncRoot != bad.SyncRoot)
hsh1["SyncRoot"] = "";
//CopyTo
string[] iArr1 = null;
string[] iArr2 = null;
DictionaryEntry[] dicEnt1;
//CopyTo() copies the values!!!
iArr1 = new string[good.Count];
iArr2 = new string[good.Count];
bad.CopyTo(iArr2, 0);
if (dic == DicType.Value)
{
dicEnt1 = new DictionaryEntry[good.Count];
good.CopyTo(dicEnt1, 0);
for (int i = 0; i < good.Count; i++)
iArr1[i] = (string)((DictionaryEntry)dicEnt1[i]).Value;
for (int i = 0; i < iArr1.Length; i++)
{
if (!iArr1[i].Equals(iArr2[i]))
hsh1["CopyTo"] = "vanila";
}
iArr1 = new string[good.Count + 5];
iArr2 = new string[good.Count + 5];
for (int i = 0; i < good.Count; i++)
iArr1[i + 5] = (string)((DictionaryEntry)dicEnt1[i]).Value;
bad.CopyTo(iArr2, 5);
for (int i = 5; i < iArr1.Length; i++)
{
if (!iArr1[i].Equals(iArr2[i]))
{
hsh1["CopyTo"] = "5";
}
}
}
else if (dic == DicType.Key)
{
for (int i = 0; i < iArr1.Length; i++)
{
if (!good.Contains(iArr2[i]))
hsh1["CopyTo"] = "Key";
}
}
try
{
bad.CopyTo(iArr2, -1);
hsh1["CopyTo"] = "";
}
catch (ArgumentException)
{
}
catch (Exception ex)
{
hsh1["CopyTo"] = ex;
}
try
{
bad.CopyTo(null, 5);
hsh1["CopyTo"] = "";
}
catch (ArgumentNullException)
{
}
catch (Exception ex)
{
hsh1["CopyTo"] = ex;
}
//Enumerator
IEnumerator ienm1;
IEnumerator ienm2;
ienm1 = good.GetEnumerator();
ienm2 = bad.GetEnumerator();
DoTheEnumerator(ienm1, ienm2, hsh1, dic, good);
}