在 C# 数组的指定索引处复制 StringCollection


想要复制数组中指定索引处的 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

Learn C# in-depth with real-world projects through our C# certification course. Enroll and become a certified expert to boost your career.

示例

现在让我们看另一个示例 -

 在线演示

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

更新于: 10-12-2019

78 次浏览

开启您的职业生涯

通过完成课程获得认证

开始学习
广告