本文整理汇总了C#中SortedList.Clone方法的典型用法代码示例。如果您正苦于以下问题:C# SortedList.Clone方法的具体用法?C# SortedList.Clone怎么用?C# SortedList.Clone使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SortedList
的用法示例。
在下文中一共展示了SortedList.Clone方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Clone_IsShallowCopy
public static void Clone_IsShallowCopy()
{
var sortList = new SortedList();
for (int i = 0; i < 10; i++)
{
sortList.Add(i, new Foo());
}
SortedList sortListClone = (SortedList)sortList.Clone();
string stringValue = "Hello World";
for (int i = 0; i < 10; i++)
{
Assert.Equal(stringValue, ((Foo)sortListClone[i]).StringValue);
}
// Now we remove an object from the original list, but this should still be present in the clone
sortList.RemoveAt(9);
Assert.Equal(stringValue, ((Foo)sortListClone[9]).StringValue);
stringValue = "Good Bye";
((Foo)sortList[0]).StringValue = stringValue;
Assert.Equal(stringValue, ((Foo)sortList[0]).StringValue);
Assert.Equal(stringValue, ((Foo)sortListClone[0]).StringValue);
// If we change the object, of course, the previous should not happen
sortListClone[0] = new Foo();
Assert.Equal(stringValue, ((Foo)sortList[0]).StringValue);
stringValue = "Hello World";
Assert.Equal(stringValue, ((Foo)sortListClone[0]).StringValue);
}
示例2: runTest
public bool runTest()
{
Console.WriteLine(s_strTFPath + " " + s_strTFName + " , for " + s_strClassMethod + " , Source ver : " + s_strDtTmVer);
int iCountErrors = 0;
int iCountTestcases = 0;
String strLoc = "Loc_000oo";
SortedList hsh1;
SortedList hsh2;
String strKey;
String strValue;
try
{
do
{
strLoc = "Loc_8345vdfv";
hsh1 = new SortedList();
for(int i=0; i<10; i++)
{
strKey = "Key_" + i;
strValue = "String_" + i;
hsh1.Add(strKey, strValue);
}
hsh2 = (SortedList)hsh1.Clone();
iCountTestcases++;
for(int i=0; i<10; i++)
{
strValue = "String_" + i;
if(!strValue.Equals((String)hsh2["Key_" + i]))
{
iCountErrors++;
Console.WriteLine("Err_561dvs_" + i + "! Expected value not returned, " + strValue);
}
}
hsh1.Remove("Key_9");
iCountTestcases++;
if(hsh1["Key_9"]!=null)
{
iCountErrors++;
Console.WriteLine("Err_563vdf! Expected value not returned, <<" + hsh1["Key_9"] + ">>");
}
strValue = "String_" + 9;
iCountTestcases++;
if(!strValue.Equals((String)hsh2["Key_9"]))
{
iCountErrors++;
Console.WriteLine("Err_563vdf! Expected value not returned, <<" + hsh1[9] + ">>");
}
strLoc = "Loc_625fd";
hsh1 = new SortedList(1000);
hsh2 = (SortedList)hsh1.Clone();
iCountTestcases++;
if(hsh1.Count != hsh2.Count)
{
iCountErrors++;
Console.WriteLine("Err_453gfd! Expected value not returned, <<" + hsh1.Count + ">> <<"+ hsh2.Count + ">>");
}
iCountTestcases++;
if(hsh1.IsReadOnly != hsh2.IsReadOnly)
{
iCountErrors++;
Console.WriteLine("Err_5234vsfd! Expected value not returned, <<" + hsh1.IsReadOnly + ">> <<"+ hsh2.IsReadOnly + ">>");
}
iCountTestcases++;
if(hsh1.IsSynchronized != hsh2.IsSynchronized)
{
iCountErrors++;
Console.WriteLine("Err_5234vsfd! Expected value not returned, <<" + hsh1.IsSynchronized + ">> <<"+ hsh2.IsSynchronized + ">>");
}
strLoc = "Loc_8345vdfv";
hsh1 = new SortedList();
for(int i=0; i<10; i++)
{
hsh1.Add(i, new Foo());
}
hsh2 = (SortedList)hsh1.Clone();
iCountTestcases++;
for(int i=0; i<10; i++)
{
strValue = "Hello World";
if(!strValue.Equals(((Foo)hsh2[i]).strValue))
{
iCountErrors++;
Console.WriteLine("Err_5623rvsdf_" + i + "! Expected value not returned, " + strValue);
}
}
strValue = "Good Bye";
((Foo)hsh1[0]).strValue = strValue;
iCountTestcases++;
if(!strValue.Equals(((Foo)hsh1[0]).strValue))
{
iCountErrors++;
Console.WriteLine("Err_7453bvdf! Expected value not returned, " + strValue);
}
iCountTestcases++;
if(!strValue.Equals(((Foo)hsh2[0]).strValue))
{
iCountErrors++;
Console.WriteLine("Err_7453bvdf! Expected value not returned, " + strValue);
}
hsh2[0] = new Foo();
//.........这里部分代码省略.........
示例3: PerformActionOnAllSortedListWrappers
public static void PerformActionOnAllSortedListWrappers(SortedList sortedList, Action<SortedList> action)
{
// Synchronized returns a slightly different version of Stack
SortedList[] sortedListTypes =
{
(SortedList)sortedList.Clone(),
SortedList.Synchronized(sortedList)
};
foreach (SortedList sortedListType in sortedListTypes)
{
action(sortedListType);
}
}
示例4: TestCloneBasic
public void TestCloneBasic()
{
SortedList hsh1;
SortedList hsh2;
string strKey;
string strValue;
// Vanila test case - Clone should exactly replicate a collection to another object reference
//afterwards these 2 should not hold the same object references
hsh1 = new SortedList();
for (int i = 0; i < 10; i++)
{
strKey = "Key_" + i;
strValue = "String_" + i;
hsh1.Add(strKey, strValue);
}
hsh2 = (SortedList)hsh1.Clone();
for (int i = 0; i < 10; i++)
{
strValue = "String_" + i;
Assert.Equal(strValue, (string)hsh2["Key_" + i]);
}
//now we remove an object from the original list
hsh1.Remove("Key_9");
Assert.Null(hsh1["Key_9"]);
Assert.Equal("String_9", (string)hsh2["Key_9"]);
//now we try other test cases
//are all the 'other' properties of the SortedList the same?
hsh1 = new SortedList(1000);
hsh2 = (SortedList)hsh1.Clone();
Assert.Equal(hsh1.Count, hsh2.Count);
Assert.Equal(hsh1.IsReadOnly, hsh2.IsReadOnly);
Assert.Equal(hsh1.IsSynchronized, hsh2.IsSynchronized);
//Clone is a shallow copy, so the objects of the objets reference should be the same
hsh1 = new SortedList();
for (int i = 0; i < 10; i++)
{
hsh1.Add(i, new Foo());
}
hsh2 = (SortedList)hsh1.Clone();
for (int i = 0; i < 10; i++)
{
strValue = "Hello World";
Assert.Equal(strValue, ((Foo)hsh2[i]).strValue);
}
strValue = "Good Bye";
((Foo)hsh1[0]).strValue = strValue;
Assert.Equal(strValue, ((Foo)hsh1[0]).strValue);
//da test
Assert.Equal(strValue, ((Foo)hsh2[0]).strValue);
//if we change the object, of course, the previous should not happen
hsh2[0] = new Foo();
strValue = "Good Bye";
Assert.Equal(strValue, ((Foo)hsh1[0]).strValue);
strValue = "Hello World";
Assert.Equal(strValue, ((Foo)hsh2[0]).strValue);
}