本文整理汇总了C#中Hashtable.CopyTo方法的典型用法代码示例。如果您正苦于以下问题:C# Hashtable.CopyTo方法的具体用法?C# Hashtable.CopyTo怎么用?C# Hashtable.CopyTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Hashtable
的用法示例。
在下文中一共展示了Hashtable.CopyTo方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestHashTableCopyToEmpty
// test CopyTo on a empty HashTable
public void TestHashTableCopyToEmpty ()
{
Hashtable hashTable = new Hashtable ();
AssertEquals ("count", 0, hashTable.Count);
object[] array = new object [hashTable.Count];
hashTable.CopyTo (array, 0);
}
示例2: TestCtorIntSingle
public void TestCtorIntSingle()
{
// variables used for tests
Hashtable hash = null;
// [] should get ArgumentException if trying to have large num of entries
Assert.Throws<ArgumentException>(() =>
{
hash = new Hashtable(int.MaxValue, .1f);
}
);
// []should not get any exceptions for valid values - we also check that the HT works here
hash = new Hashtable(100, .1f);
int iNumberOfElements = 100;
for (int i = 0; i < iNumberOfElements; i++)
{
hash.Add("Key_" + i, "Value_" + i);
}
//Count
Assert.Equal(hash.Count, iNumberOfElements);
DictionaryEntry[] strValueArr = new DictionaryEntry[hash.Count];
hash.CopyTo(strValueArr, 0);
Hashtable hsh3 = new Hashtable();
for (int i = 0; i < iNumberOfElements; i++)
{
Assert.True(hash.Contains("Key_" + i), "Error, Expected value not returned, " + hash.Contains("Key_" + i));
Assert.True(hash.ContainsKey("Key_" + i), "Error, Expected value not returned, " + hash.ContainsKey("Key_" + i));
Assert.True(hash.ContainsValue("Value_" + i), "Error, Expected value not returned, " + hash.ContainsValue("Value_" + i));
//we still need a way to make sure that there are all these unique values here -see below code for that
Assert.True(hash.ContainsValue(((DictionaryEntry)strValueArr[i]).Value), "Error, Expected value not returned, " + ((DictionaryEntry)strValueArr[i]).Value);
hsh3.Add(((DictionaryEntry)strValueArr[i]).Value, null);
}
}
示例3: runTest
public Boolean runTest()
{
Console.WriteLine( s_strTFPath +" "+ s_strTFName +" ,for "+ s_strClassMethod +" ,Source ver "+ s_strDtTmVer );
String strLoc = "Loc_000oo";
Hashtable hash = null;
int iCountErrors = 0;
int iCountTestcases = 0;
strLoc = "Loc_001oo";
try
{
++iCountTestcases;
hash = new Hashtable( Int32.MaxValue, .1f, null, null );
++iCountErrors;
Console.WriteLine( s_strTFAbbrev +" Error_0001! hashtable should have thrown ArgumentException here" );
}
catch( ArgumentException )
{
}
catch( Exception ex )
{
++iCountErrors;
Console.WriteLine( s_strTFAbbrev +" Error_1001! we expected ArgumentException but got exception " + ex.ToString() );
}
try
{
++iCountTestcases;
hash = new Hashtable( 100, .1f, null, null );
}
catch( Exception ex )
{
++iCountErrors;
Console.WriteLine( s_strTFAbbrev +" Error_1002! we expected no exception but got exception " + ex.ToString() );
}
Int32 iNumberOfElements = 100;
for(int i=0; i<iNumberOfElements; i++)
{
hash.Add("Key_" + i, "Value_" + i);
}
iCountTestcases++;
if(hash.Count != iNumberOfElements)
{
iCountErrors++;
Console.WriteLine("Err_742dsf! Expected value not returned, " + hash.Count);
}
DictionaryEntry[] strValueArr = new DictionaryEntry[hash.Count];
hash.CopyTo(strValueArr, 0);
Hashtable hsh3 = new Hashtable();
for(int i=0; i<iNumberOfElements; i++)
{
if(!hash.Contains("Key_" + i))
{
iCountErrors++;
Console.WriteLine("Err_742ds8f! Expected value not returned, " + hash.Contains("Key_" + i));
}
if(!hash.ContainsKey("Key_" + i))
{
iCountErrors++;
Console.WriteLine("Err_742389dsaf! Expected value not returned, " + hash.ContainsKey("Key_" + i));
}
if(!hash.ContainsValue("Value_" + i))
{
iCountErrors++;
Console.WriteLine("Err_563fgd! Expected value not returned, " + hash.ContainsValue("Value_" + i));
}
if(!hash.ContainsValue(((DictionaryEntry)strValueArr[i]).Value))
{
iCountErrors++;
Console.WriteLine("Err_87429dsfd! Expected value not returned, " + ((DictionaryEntry)strValueArr[i]).Value);
}
try
{
hsh3.Add(((DictionaryEntry)strValueArr[i]).Value, null);
}
catch(Exception)
{
iCountErrors++;
Console.WriteLine("Err_74298dsd! Exception thrown for " + ((DictionaryEntry)strValueArr[i]).Value);
}
}
try
{
++iCountTestcases;
hash = new Hashtable( 5, .01f, null, null );
++iCountErrors;
Console.WriteLine( s_strTFAbbrev +" Error_0003! hashtable should have thrown ArgumentOutOfRangeException here" );
}
catch( ArgumentOutOfRangeException )
{
}
catch( Exception ex )
{
++iCountErrors;
Console.WriteLine( s_strTFAbbrev +" Error_1003! we expected ArgumentOutOfRangeException but got exception " + ex.ToString() );
}
try
{
++iCountTestcases;
hash = new Hashtable( 5, 100.1f, null, null );
++iCountErrors;
Console.WriteLine( s_strTFAbbrev +" Error_0004! hashtable should have thrown ArgumentOutOfRangeException here" );
//.........这里部分代码省略.........
示例4: TestSynchronizedBasic
public void TestSynchronizedBasic()
{
Hashtable hsh1;
string strValue;
Task[] workers;
Action ts1;
int iNumberOfWorkers = 3;
DictionaryEntry[] strValueArr;
string[] strKeyArr;
Hashtable hsh3;
Hashtable hsh4;
IDictionaryEnumerator idic;
object oValue;
//[]Vanila - Syncronized returns a wrapped HT. We must make sure that all the methods
//are accounted for here for the wrapper
hsh1 = new Hashtable();
for (int i = 0; i < _iNumberOfElements; i++)
{
hsh1.Add("Key_" + i, "Value_" + i);
}
_hsh2 = Hashtable.Synchronized(hsh1);
//Count
Assert.Equal(_hsh2.Count, hsh1.Count);
//get/set item
for (int i = 0; i < _iNumberOfElements; i++)
{
Assert.True(((string)_hsh2["Key_" + i]).Equals("Value_" + i));
}
Assert.Throws<ArgumentNullException>(() =>
{
oValue = _hsh2[null];
});
_hsh2.Clear();
for (int i = 0; i < _iNumberOfElements; i++)
{
_hsh2["Key_" + i] = "Value_" + i;
}
strValueArr = new DictionaryEntry[_hsh2.Count];
_hsh2.CopyTo(strValueArr, 0);
//ContainsXXX
hsh3 = new Hashtable();
for (int i = 0; i < _iNumberOfElements; i++)
{
Assert.True(_hsh2.Contains("Key_" + i));
Assert.True(_hsh2.ContainsKey("Key_" + i));
Assert.True(_hsh2.ContainsValue("Value_" + i));
//we still need a way to make sure that there are all these unique values here -see below code for that
Assert.True(hsh1.ContainsValue(((DictionaryEntry)strValueArr[i]).Value));
hsh3.Add(strValueArr[i], null);
}
hsh4 = (Hashtable)_hsh2.Clone();
Assert.Equal(hsh4.Count, hsh1.Count);
strValueArr = new DictionaryEntry[hsh4.Count];
hsh4.CopyTo(strValueArr, 0);
//ContainsXXX
hsh3 = new Hashtable();
for (int i = 0; i < _iNumberOfElements; i++)
{
Assert.True(hsh4.Contains("Key_" + i));
Assert.True(hsh4.ContainsKey("Key_" + i));
Assert.True(hsh4.ContainsValue("Value_" + i));
//we still need a way to make sure that there are all these unique values here -see below code for that
Assert.True(hsh1.ContainsValue(((DictionaryEntry)strValueArr[i]).Value));
hsh3.Add(((DictionaryEntry)strValueArr[i]).Value, null);
}
Assert.False(hsh4.IsReadOnly);
Assert.True(hsh4.IsSynchronized);
//Phew, back to other methods
idic = _hsh2.GetEnumerator();
hsh3 = new Hashtable();
hsh4 = new Hashtable();
while (idic.MoveNext())
{
Assert.True(_hsh2.ContainsKey(idic.Key));
Assert.True(_hsh2.ContainsValue(idic.Value));
hsh3.Add(idic.Key, null);
hsh4.Add(idic.Value, null);
}
hsh4 = (Hashtable)_hsh2.Clone();
strValueArr = new DictionaryEntry[hsh4.Count];
hsh4.CopyTo(strValueArr, 0);
hsh3 = new Hashtable();
//.........这里部分代码省略.........
示例5: TestCopyToBasic
public void TestCopyToBasic()
{
Hashtable hash = null; // the hashtable object which will be used in the tests
object[] objArr = null; // the object array corresponding to arr
object[] objArr2 = null; // helper object array
object[,] objArrRMDim = null; // multi dimensional object array
// these are the keys and values which will be added to the hashtable in the tests
object[] keys = new object[] {
new object(),
"Hello" ,
"my array" ,
new DateTime(),
new SortedList(),
typeof( System.Environment ),
5
};
object[] values = new object[] {
"Somestring" ,
new object(),
new int [] { 1, 2, 3, 4, 5 },
new Hashtable(),
new Exception(),
new Hashtable_CopyToTests(),
null
};
//[]test normal conditions, array is large enough to hold all elements
// make new hashtable
hash = new Hashtable();
// put in values and keys
for (int i = 0; i < values.Length; i++)
{
hash.Add(keys[i], values[i]);
}
// now try getting out the values using CopyTo method
objArr = new object[values.Length + 2];
// put a sentinal in first position, and make sure it is not overriden
objArr[0] = "startstring";
// put a sentinal in last position, and make sure it is not overriden
objArr[values.Length + 1] = "endstring";
hash.Values.CopyTo((Array)objArr, 1);
// make sure sentinal character is still there
Assert.Equal("startstring", objArr[0]);
Assert.Equal("endstring", objArr[values.Length + 1]);
// check to make sure arr is filled up with the correct elements
objArr2 = new object[values.Length];
Array.Copy(objArr, 1, objArr2, 0, values.Length);
objArr = objArr2;
Assert.True(CompareArrays(objArr, values));
//[] This is the same test as before but now we are going to used Hashtable.CopyTo instead of Hasthabe.Values.CopyTo
// now try getting out the values using CopyTo method
objArr = new object[values.Length + 2];
// put a sentinal in first position, and make sure it is not overriden
objArr[0] = "startstring";
// put a sentinal in last position, and make sure it is not overriden
objArr[values.Length + 1] = "endstring";
hash.CopyTo((Array)objArr, 1);
// make sure sentinal character is still there
Assert.Equal("startstring", objArr[0]);
Assert.Equal("endstring", objArr[values.Length + 1]);
// check to make sure arr is filled up with the correct elements
BitArray bitArray = new BitArray(values.Length);
for (int i = 0; i < values.Length; i++)
{
DictionaryEntry entry = (DictionaryEntry)objArr[i + 1];
int valueIndex = Array.IndexOf(values, entry.Value);
int keyIndex = Array.IndexOf(keys, entry.Key);
Assert.NotEqual(-1, valueIndex);
Assert.NotEqual(-1, keyIndex);
Assert.Equal(valueIndex, keyIndex);
bitArray[i] = true;
}
for (int i = 0; i < ((ICollection)bitArray).Count; i++)
{
Assert.True(bitArray[i]);
}
//[] Parameter validation
//[] Null array
//.........这里部分代码省略.........
示例6: 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";
Hashtable hsh1;
String strValue;
Thread[] workers;
ThreadStart ts1;
Int32 iNumberOfWorkers = 15;
Boolean fLoopExit;
DictionaryEntry[] strValueArr;
String[] strKeyArr;
Hashtable hsh3;
Hashtable hsh4;
IDictionaryEnumerator idic;
MemoryStream ms1;
Boolean fPass;
Object oValue;
try
{
do
{
hsh1 = new Hashtable();
for(int i=0; i<iNumberOfElements; i++)
{
hsh1.Add("Key_" + i, "Value_" + i);
}
hsh2 = Hashtable.Synchronized(hsh1);
fPass = true;
iCountTestcases++;
if(hsh2.Count != hsh1.Count)
{
iCountErrors++;
Console.WriteLine("Err_742dsf! Expected value not returned, " + hsh2.Count);
}
for(int i=0; i<iNumberOfElements; i++)
{
if(!((String)hsh2["Key_" + i]).Equals("Value_" + i))
{
Console.WriteLine(hsh2["Key_" + i]);
fPass = false;
}
}
try
{
oValue = hsh2[null];
fPass = false;
}
catch(ArgumentNullException)
{
}
catch(Exception)
{
fPass = false;
}
hsh2.Clear();
for(int i=0; i<iNumberOfElements; i++)
{
hsh2["Key_" + i] = "Value_" + i;
}
if(!fPass)
{
iCountErrors++;
Console.WriteLine("Err_752dsgf! Oh man! This is busted!!!!");
}
strValueArr = new DictionaryEntry[hsh2.Count];
hsh2.CopyTo(strValueArr, 0);
hsh3 = new Hashtable();
for(int i=0; i<iNumberOfElements; i++)
{
if(!hsh2.Contains("Key_" + i))
{
iCountErrors++;
Console.WriteLine("Err_742ds8f! Expected value not returned, " + hsh2.Contains("Key_" + i));
}
if(!hsh2.ContainsKey("Key_" + i))
{
iCountErrors++;
Console.WriteLine("Err_742389dsaf! Expected value not returned, " + hsh2.ContainsKey("Key_" + i));
}
if(!hsh2.ContainsValue("Value_" + i))
{
iCountErrors++;
Console.WriteLine("Err_563fgd! Expected value not returned, " + hsh2.ContainsValue("Value_" + i));
}
if(!hsh1.ContainsValue(((DictionaryEntry)strValueArr[i]).Value))
{
iCountErrors++;
Console.WriteLine("Err_87429dsfd! Expected value not returned, " + ((DictionaryEntry)strValueArr[i]).Value);
}
try
{
hsh3.Add(strValueArr[i], null);
}
catch(Exception)
{
iCountErrors++;
Console.WriteLine("Err_74298dsd! Exception thrown for " + strValueArr[i]);
//.........这里部分代码省略.........
示例7: runTest
public Boolean runTest()
{
Console.Error.WriteLine( s_strTFPath + " " + s_strTFName + " , for " + s_strComponentBeingTested + " ,Source ver " + s_strDtTmVer );
String strLoc = "Loc_000ooo";
int iCountTestcases = 0;
int iCountErrors = 0;
Hashtable hash = null;
Object [] objArr = null;
Object [] objArr2 = null;
Object [][] objArrMDim = null;
Object [,] objArrRMDim = null;
Object [] keys = new Object [] {
new Object(),
"Hello" ,
"my array" ,
new DateTime(),
new JulianCalendar(),
typeof( System.Environment ),
5
};
Object [] values = new Object[] {
"SomeString" ,
new Object(),
new int [] { 1, 2, 3, 4, 5 },
new Hashtable(),
new Exception(),
new Co1657CopyTo_ai(),
null
};
if ( verbose ) Console.WriteLine( "test normal conditions, array is large enough to hold all elements" );
try
{
strLoc = "Err_0001a";
++iCountTestcases;
hash = new Hashtable();
for ( int i = 0; i < values.Length; i++ )
{
hash.Add( keys[i], values[i] );
}
objArr = new Object [values.Length + 2];
objArr[0] = "startstring" ;
objArr[values.Length+1] = "endstring" ;
hash.Values.CopyTo( (Array)objArr, 1 );
if ( ! "startstring".Equals( objArr[0] ) )
{
++iCountErrors;
Console.WriteLine( "Location: Err_0001b" );
Console.WriteLine( "StartSentinal was overwritten by something" );
}
else if ( ! "endstring".Equals( objArr[values.Length+1] ) )
{
++iCountErrors;
Console.WriteLine( "Location: Err_0001c" );
Console.WriteLine( "EndSentinal was overwritten by something" );
}
objArr2 = new Object[ values.Length ];
Array.Copy( objArr, 1, objArr2, 0, values.Length );
objArr = objArr2;
if ( ! CompareArrays( objArr, values ) )
{
++iCountErrors;
Console.WriteLine( "Location: Err_0001d" );
Console.WriteLine( "values do not match values which were inserted" );
}
try
{
++iCountTestcases;
hash = new Hashtable();
objArr = new Object[0];
hash.CopyTo( objArr, Int32.MaxValue );
++iCountErrors;
Console.WriteLine( "Err_015a, Expected ArgumentException but no exception thrown" );
}
catch ( ArgumentException )
{
}
catch (Exception ex)
{
++iCountErrors;
Console.WriteLine( "Err_015b, Expected ArgumentException but exception thrown= " + ex.ToString() );
}
try
{
++iCountTestcases;
hash = new Hashtable();
objArr = new Object[0];
hash.CopyTo( objArr, Int32.MinValue );
++iCountErrors;
Console.WriteLine( "Err_015a, Expected ArgumentException but no exception thrown" );
}
catch ( ArgumentException )
{
}
catch (Exception ex)
{
++iCountErrors;
Console.WriteLine( "Err_015b, Expected ArgumentException but exception thrown= " + ex.ToString() );
}
if ( verbose ) Console.WriteLine( "copy should throw because of outofrange" );
Random random = new Random();
//.........这里部分代码省略.........