배열의 지정된 인덱스에서 StringCollection을 복사하려면 코드는 다음과 같습니다. -
예
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
StringCollection strCol = new StringCollection();
String[] strArr = new String[] { "Tim", "Tom", "Sam", "Mark", "Katie", "Jacob"};
Console.WriteLine("Elements...");
for (int i = 0; i < strArr.Length; i++) {
Console.WriteLine(strArr[i]);
}
strCol.AddRange(strArr);
String[] arr = new String[strCol.Count];
strCol.CopyTo(arr, 0);
Console.WriteLine("Elements...after copying StringCollection to array");
for (int i = 0; i < arr.Length; i++) {
Console.WriteLine(arr[i]);
}
}
} 출력
이것은 다음과 같은 출력을 생성합니다 -
Elements... Tim Tom Sam Mark Katie Jacob Elements...after copying StringCollection to array Tim Tom Sam Mark Katie Jacob
예
이제 다른 예를 살펴보겠습니다 -
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
StringCollection strCol = new StringCollection();
String[] strArr = new String[] { "Tim", "Tom", "Sam", "Mark", "Katie", "Jacob", "David"};
Console.WriteLine("Elements...");
for (int i = 0; i < strArr.Length; i++) {
Console.WriteLine(strArr[i]);
}
strCol.AddRange(strArr);
String[] arr = new String[10];
strCol.CopyTo(arr, 3);
Console.WriteLine("Elements...after copying");
for (int i = 0; i < arr.Length; i++) {
Console.WriteLine(arr[i]);
}
}
} 출력
이것은 다음과 같은 출력을 생성합니다 -
Elements... Tim Tom Sam Mark Katie Jacob David Elements...after copying Tim Tom Sam Mark Katie Jacob David