本文整理汇总了C#中System.Collections.Hashtable.CopyTo方法的典型用法代码示例。如果您正苦于以下问题:C# Hashtable.CopyTo方法的具体用法?C# Hashtable.CopyTo怎么用?C# Hashtable.CopyTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.Hashtable
的用法示例。
在下文中一共展示了Hashtable.CopyTo方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Reload
protected override void Reload ()
{
System.Collections.Hashtable ht = new System.Collections.Hashtable ();
Photo [] photos = query.Store.Query ((Tag [])null, null, null, null);
foreach (Photo p in photos) {
if (ht.Contains (p.DirectoryPath)) {
DirectoryAdaptor.Group group = (DirectoryAdaptor.Group) ht [p.DirectoryPath];
group.Count += 1;
} else
ht [p.DirectoryPath] = new DirectoryAdaptor.Group ();
}
Console.WriteLine ("Count = {0}", ht.Count);
dirs = new System.Collections.DictionaryEntry [ht.Count];
ht.CopyTo (dirs, 0);
Array.Sort (dirs, new DirectoryAdaptor.Group ());
Array.Sort (query.Photos, new Photo.CompareDirectory ());
if (!order_ascending) {
Array.Reverse (dirs);
Array.Reverse (query.Photos);
}
if (Changed != null)
Changed (this);
}
示例2: applyKeepWords
public static Hashtable applyKeepWords(Hashtable centroidValues, int keepWords)
{
DictionaryEntry[] centValuesArr = new DictionaryEntry[centroidValues.Count];
centroidValues.CopyTo(centValuesArr, 0);
Array.Sort(centValuesArr, new DictionaryEntryValueComparer());
Array.Reverse(centValuesArr);
Hashtable finalCentroidValues = new Hashtable();
for (int i = 0; i < keepWords && i < centValuesArr.Length; i++)
{
DictionaryEntry entry = centValuesArr[i];
finalCentroidValues.Add(entry.Key, entry.Value);
}
return (finalCentroidValues);
}
示例3: 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 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
//.........这里部分代码省略.........
示例4: CopyTo_Empty
public void CopyTo_Empty ()
{
Hashtable ht = new Hashtable ();
Assert.AreEqual (0, ht.Count, "Count");
object[] array = new object [ht.Count];
ht.CopyTo (array, 0);
}
示例5: TestCopyTo
public void TestCopyTo() {
{
bool errorThrown = false;
try {
Hashtable h = new Hashtable();
h.CopyTo(null, 0);
} catch (ArgumentNullException e) {
errorThrown = true;
Assert.AreEqual ("array", e.ParamName, "ParamName is not \"array\"");
}
Assert.IsTrue (errorThrown, "null hashtable error not thrown");
}
{
bool errorThrown = false;
try {
Hashtable h = new Hashtable();
Object[] o = new Object[1];
h.CopyTo(o, -1);
} catch (ArgumentOutOfRangeException e) {
errorThrown = true;
Assert.AreEqual ("arrayIndex", e.ParamName, "ParamName is not \"arrayIndex\"");
}
Assert.IsTrue (errorThrown, "out of range error not thrown");
}
{
bool errorThrown = false;
try {
Hashtable h = new Hashtable();
Object[,] o = new Object[1,1];
h.CopyTo(o, 1);
} catch (ArgumentException) {
errorThrown = true;
}
Assert.IsTrue (errorThrown, "multi-dim array error not thrown");
}
{
bool errorThrown = false;
try {
Hashtable h = new Hashtable();
h['a'] = 1; // no error if table is empty
Object[] o = new Object[5];
h.CopyTo(o, 5);
} catch (ArgumentException) {
errorThrown = true;
}
Assert.IsTrue (errorThrown, "no room in array error not thrown");
}
{
bool errorThrown = false;
try {
Hashtable h = new Hashtable();
h['a'] = 1;
h['b'] = 2;
h['c'] = 2;
Object[] o = new Object[2];
h.CopyTo(o, 0);
} catch (ArgumentException) {
errorThrown = true;
}
Assert.IsTrue (errorThrown, "table too big error not thrown");
}
{
bool errorThrown = false;
try {
Hashtable h = new Hashtable();
h["blue"] = 1;
h["green"] = 2;
h["red"] = 3;
Char[] o = new Char[3];
h.CopyTo(o, 0);
} catch (InvalidCastException) {
errorThrown = true;
}
Assert.IsTrue (errorThrown, "invalid cast error not thrown");
}
{
Hashtable h = new Hashtable();
h['a'] = 1;
h['b'] = 2;
DictionaryEntry[] o = new DictionaryEntry[2];
h.CopyTo(o,0);
Assert.AreEqual ('a', o[0].Key, "first copy fine.");
Assert.AreEqual (1, o[0].Value, "first copy fine.");
Assert.AreEqual ('b', o[1].Key, "second copy fine.");
Assert.AreEqual (2, o[1].Value, "second copy fine.");
}
}
示例6: NonGenericDictionarySource
public void NonGenericDictionarySource()
{
var source = new Hashtable();
// guid
var guidValue = new Guid("21EC2020-3AEA-1069-A2DD-08002B30309D");
source["GuidPty"] = guidValue;
Assert.AreEqual(guidValue, source.CopyTo<TargetType>().GuidPty);
source["GuidPty"] = guidValue.ToString();
Assert.AreEqual(guidValue, source.CopyTo<TargetType>().GuidPty);
source["GuidPty"] = guidValue.ToByteArray();
Assert.AreEqual(guidValue, source.CopyTo<TargetType>().GuidPty);
// int
source["IntPty"] = 345;
Assert.AreEqual(345, source.CopyTo<TargetType>().IntPty);
source["IntPty"] = 345.ToString(CultureInfo.InvariantCulture);
Assert.AreEqual(345, source.CopyTo<TargetType>().IntPty);
source["IntPty"] = (long)345;
Assert.AreEqual(345, source.CopyTo<TargetType>().IntPty);
source["IntPty"] = int.MaxValue;
Assert.AreEqual(int.MaxValue, source.CopyTo<TargetType>().IntPty);
// enum pty
source["EnumPty"] = TargetType.TestEnum.Three;
Assert.AreEqual(TargetType.TestEnum.Three, source.CopyTo<TargetType>().EnumPty);
source["EnumPty"] = TargetType.TestEnum.Three.ToString();
Assert.AreEqual(TargetType.TestEnum.Three, source.CopyTo<TargetType>().EnumPty);
source["EnumPty"] = (int)TargetType.TestEnum.Three;
Assert.AreEqual(TargetType.TestEnum.Three, source.CopyTo<TargetType>().EnumPty);
source["EnumPty"] = (byte)TargetType.TestEnum.Three;
Assert.AreEqual(TargetType.TestEnum.Three, source.CopyTo<TargetType>().EnumPty);
source["EnumPty"] = (long)TargetType.TestEnum.Three;
Assert.AreEqual(TargetType.TestEnum.Three, source.CopyTo<TargetType>().EnumPty);
// obj pty
var obj = new object();
source["ObjPty"] = obj;
Assert.AreEqual(obj, source.CopyTo<TargetType>().ObjPty);
}
示例7: CopyTo_Empty
public void CopyTo_Empty ()
{
Hashtable ht = new Hashtable ();
AssertEquals ("Count", 0, ht.Count);
object[] array = new object [ht.Count];
ht.CopyTo (array, 0);
}
示例8: 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();
//.........这里部分代码省略.........
示例9: 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);
}
}
示例10: ElementAtShouldReturnExpectedValueFromDictionary
public void ElementAtShouldReturnExpectedValueFromDictionary()
{
var target = new Hashtable();
target.Add( 0, new object() );
target.Add( 1, new object() );
target.Add( 2, new object() );
var index = 2;
var array = Array.CreateInstance( typeof( DictionaryEntry ), 3 );
target.CopyTo( array, 0 );
var expected = array.GetValue( index );
var actual = target.ElementAt( index );
Assert.Equal( expected, actual );
}