本文整理汇总了C#中SortedList.CopyTo方法的典型用法代码示例。如果您正苦于以下问题:C# SortedList.CopyTo方法的具体用法?C# SortedList.CopyTo怎么用?C# SortedList.CopyTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SortedList
的用法示例。
在下文中一共展示了SortedList.CopyTo方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}
示例2: runTest
//.........这里部分代码省略.........
}
catch(ArgumentOutOfRangeException)
{
}
catch(Exception ex)
{
iCountErrors++;
Console.WriteLine("Err_0155234sfdg! Unexpected exception thrown, " + ex);
}
try
{
iCountTestcases++;
slst1.SetByIndex(slst1.Count, strValue);
iCountErrors++;
Console.WriteLine("Err_7439dg! Exception not thrown");
}
catch(ArgumentOutOfRangeException)
{
}
catch(Exception ex)
{
iCountErrors++;
Console.WriteLine("Err_7320678dfv! Unexpected exception thrown, " + ex);
}
strLoc = "Loc_0734tefgd";
iCountTestcases++;
iNumberOfElements = 10;
slst1 = new SortedList();
for(int i=iNumberOfElements-1; i>=0;i--)
{
slst1.Add(50 + i, "Value_" + i);
}
ar1 = Array.CreateInstance(typeof(DictionaryEntry), iNumberOfElements);
slst1.CopyTo(ar1, 0);
for(int i=0; i<slst1.Count;i++)
{
strValue = "Value_" + i;
if(!strValue.Equals(((DictionaryEntry)ar1.GetValue(i)).Value))
{
iCountErrors++;
Console.WriteLine("Err_004oo! Expected value not returned, " + strValue + " " + ((DictionaryEntry)ar1.GetValue(i)).Value);
}
}
strLoc = "Loc_7645tgbfgg";
try
{
iCountTestcases++;
ar1 = Array.CreateInstance(typeof(String), iNumberOfElements);
slst1.CopyTo(ar1, 0);
iCountErrors++;
Console.WriteLine("Err_7439dg! Exception not thrown");
}
catch(InvalidCastException)
{
}
catch(Exception ex)
{
iCountErrors++;
Console.WriteLine("Err_2078sdfd! Unexpected exception thrown, " + ex);
}
try
{
iCountTestcases++;
slst1.CopyTo(null, 0);
iCountErrors++;
Console.WriteLine("Err_7439dg! Exception not thrown");
示例3: Test01
//.........这里部分代码省略.........
//paramter stuff
Assert.Throws<ArgumentNullException>(() =>
{
slst1[null] = "Not a chance";
}
);
strValue = null;
slst1[51] = strValue;
Assert.Null(slst1[51]);
//SetByIndex - this changes the value at this specific index. Note that SortedList
//does not have the equicalent Key changing means as this is a SortedList and will be done
//automatically!!!
iNumberOfElements = 10;
slst1 = new SortedList();
for (int i = iNumberOfElements - 1; i >= 0; i--)
{
slst1.Add(50 + i, "Value_" + i);
}
for (int i = 0; i < slst1.Count; i++)
{
strValue = "Value_" + i + 50;
slst1.SetByIndex(i, strValue);
}
for (int i = 0; i < slst1.Count; i++)
{
strValue = "Value_" + i + 50;
Assert.Equal(strValue, slst1.GetByIndex(i));
}
//paramter stuff
Assert.Throws<ArgumentOutOfRangeException>(() =>
{
slst1.SetByIndex(-1, strValue);
}
);
Assert.Throws<ArgumentOutOfRangeException>(() =>
{
slst1.SetByIndex(slst1.Count, strValue);
}
);
//CopyTo() - copies the values
iNumberOfElements = 10;
slst1 = new SortedList();
for (int i = iNumberOfElements - 1; i >= 0; i--)
{
slst1.Add(50 + i, "Value_" + i);
}
ar1 = new DictionaryEntry[iNumberOfElements];
slst1.CopyTo(ar1, 0);
for (int i = 0; i < slst1.Count; i++)
{
strValue = "Value_" + i;
Assert.Equal(strValue, ((DictionaryEntry)ar1.GetValue(i)).Value);
}
//paramter stuff
Assert.Throws<InvalidCastException>(() =>
{
ar1 = new String[iNumberOfElements];
slst1.CopyTo(ar1, 0);
}
);
Assert.Throws<ArgumentNullException>(() =>
{
slst1.CopyTo(null, 0);
}
);
Assert.Throws<ArgumentOutOfRangeException>(() =>
{
ar1 = new DictionaryEntry[iNumberOfElements];
slst1.CopyTo(ar1, -1);
}
);
Assert.Throws<ArgumentException>(() =>
{
ar1 = new String[iNumberOfElements];
slst1.CopyTo(ar1, 1);
}
);
ar1 = new DictionaryEntry[2 * iNumberOfElements];
slst1.CopyTo(ar1, iNumberOfElements);
for (int i = 0; i < slst1.Count; i++)
{
strValue = "Value_" + i;
Assert.Equal(strValue, ((DictionaryEntry)ar1.GetValue(iNumberOfElements + i)).Value);
}
}