本文整理汇总了C#中System.Collections.Specialized.StringDictionary.CopyTo方法的典型用法代码示例。如果您正苦于以下问题:C# StringDictionary.CopyTo方法的具体用法?C# StringDictionary.CopyTo怎么用?C# StringDictionary.CopyTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.Specialized.StringDictionary
的用法示例。
在下文中一共展示了StringDictionary.CopyTo方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
public static void Main()
{
// Creates and initializes a new StringDictionary.
StringDictionary myCol = new StringDictionary();
myCol.Add("red", "rojo");
myCol.Add("green", "verde");
myCol.Add("blue", "azul");
Console.WriteLine("Count: {0}", myCol.Count);
// Display the contents of the collection using foreach. This is the preferred method.
Console.WriteLine("Displays the elements using foreach:");
PrintKeysAndValues1(myCol);
// Display the contents of the collection using the enumerator.
Console.WriteLine("Displays the elements using the IEnumerator:");
PrintKeysAndValues2(myCol);
// Display the contents of the collection using the Keys, Values, Count, and Item properties.
Console.WriteLine("Displays the elements using the Keys, Values, Count, and Item properties:");
PrintKeysAndValues3(myCol);
// Copies the StringDictionary to an array with DictionaryEntry elements.
DictionaryEntry[] myArr = new DictionaryEntry[myCol.Count];
myCol.CopyTo(myArr, 0);
// Displays the values in the array.
Console.WriteLine("Displays the elements in the array:");
Console.WriteLine(" KEY VALUE");
for (int i = 0; i < myArr.Length; i++)
Console.WriteLine(" {0,-10} {1}", myArr[i].Key, myArr[i].Value);
Console.WriteLine();
// Searches for a value.
if (myCol.ContainsValue("amarillo"))
Console.WriteLine("The collection contains the value \"amarillo\".");
else
Console.WriteLine("The collection does not contain the value \"amarillo\".");
Console.WriteLine();
// Searches for a key and deletes it.
if (myCol.ContainsKey("green"))
myCol.Remove("green");
Console.WriteLine("The collection contains the following elements after removing \"green\":");
PrintKeysAndValues1(myCol);
// Clears the entire collection.
myCol.Clear();
Console.WriteLine("The collection contains the following elements after it is cleared:");
PrintKeysAndValues1(myCol);
}
示例2: Empty
public void Empty ()
{
StringDictionary sd = new StringDictionary ();
Assert.AreEqual (0, sd.Count, "Count");
Assert.IsFalse (sd.IsSynchronized, "IsSynchronized");
Assert.AreEqual (0, sd.Keys.Count, "Keys");
Assert.AreEqual (0, sd.Values.Count, "Values");
Assert.IsNotNull (sd.SyncRoot, "SyncRoot");
Assert.IsFalse (sd.ContainsKey ("a"), "ContainsKey");
Assert.IsFalse (sd.ContainsValue ("1"), "ContainsValue");
sd.CopyTo (new DictionaryEntry[0], 0);
Assert.IsNotNull (sd.GetEnumerator (), "GetEnumerator");
sd.Remove ("a"); // doesn't exists
sd.Clear ();
}
示例3: SomeElements
public void SomeElements ()
{
StringDictionary sd = new StringDictionary ();
for (int i = 0; i < 10; i++)
sd.Add (i.ToString (), (i * 10).ToString ());
Assert.AreEqual ("10", sd["1"], "this[1]");
Assert.AreEqual (10, sd.Count, "Count-10");
Assert.AreEqual (10, sd.Keys.Count, "Keys");
Assert.AreEqual (10, sd.Values.Count, "Values");
Assert.IsTrue (sd.ContainsKey ("2"), "ContainsKey");
Assert.IsTrue (sd.ContainsValue ("20"), "ContainsValue");
DictionaryEntry[] array = new DictionaryEntry[10];
sd.CopyTo (array, 0);
sd.Remove ("1");
Assert.AreEqual (9, sd.Count, "Count-9");
sd.Clear ();
Assert.AreEqual (0, sd.Count, "Count-0");
}
示例4: Test01
public void Test01()
{
IntlStrings intl;
StringDictionary sd;
// simple string values
string[] values =
{
"",
" ",
"a",
"aa",
"text",
" spaces",
"1",
"$%^#",
"2222222222222222222222222",
System.DateTime.Today.ToString(),
Int32.MaxValue.ToString()
};
// keys for simple string values
string[] keys =
{
"zero",
"one",
" ",
"",
"aa",
"1",
System.DateTime.Today.ToString(),
"$%^#",
Int32.MaxValue.ToString(),
" spaces",
"2222222222222222222222222"
};
Array destination;
int cnt = 0; // Count
// initialize IntStrings
intl = new IntlStrings();
// [] StringDictionary is constructed as expected
//-----------------------------------------------------------------
sd = new StringDictionary();
// [] Copy empty dictionary into empty array
//
destination = Array.CreateInstance(typeof(Object), sd.Count);
Assert.Throws<ArgumentOutOfRangeException>(() => { sd.CopyTo(destination, -1); });
sd.CopyTo(destination, 0);
Assert.Throws<ArgumentException>(() => { sd.CopyTo(destination, 1); });
// [] Copy empty dictionary into non-empty array
//
destination = Array.CreateInstance(typeof(Object), values.Length);
for (int i = 0; i < values.Length; i++)
{
destination.SetValue(values[i], i);
}
sd.CopyTo(destination, 0);
if (destination.Length != values.Length)
{
Assert.False(true, string.Format("Error, altered array after copying empty collection"));
}
if (destination.Length == values.Length)
{
for (int i = 0; i < values.Length; i++)
{
if (String.Compare(destination.GetValue(i).ToString(), values[i]) != 0)
{
Assert.False(true, string.Format("Error, altered item {0} after copying empty collection", i));
}
}
}
// [] add simple strings and CopyTo(Array, 0)
//
cnt = sd.Count;
int len = values.Length;
for (int i = 0; i < len; i++)
{
sd.Add(keys[i], values[i]);
}
if (sd.Count != len)
{
Assert.False(true, string.Format("Error, count is {0} instead of {1}", sd.Count, values.Length));
}
destination = Array.CreateInstance(typeof(Object), len);
sd.CopyTo(destination, 0);
IEnumerator en = sd.GetEnumerator();
//
// order of items is the same as order of enumerator
//
for (int i = 0; i < len; i++)
//.........这里部分代码省略.........