C#
 Computer >> コンピューター >  >> プログラミング >> C#

C#でStringCollectionの指定されたインデックスから削除します


StringCollectionの指定されたインデックスから削除するには、コードは次のとおりです-

using System;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      StringCollection stringCol = new StringCollection();
      String[] arr = new String[] { "100", "200", "300", "400", "500" };
      Console.WriteLine("Array elements...");
      foreach (string res in arr) {
         Console.WriteLine(res);
      }
      stringCol.AddRange(arr);  
      Console.WriteLine("Total number of elements = "+stringCol.Count);
      stringCol.RemoveAt(3);
      Console.WriteLine("Total number of elements now = "+stringCol.Count);
   }
}

出力

これにより、次の出力が生成されます-

Array elements...
100
200
300
400
500
Total number of elements = 5
Total number of elements now = 4

別の例を見てみましょう-

using System;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      StringCollection stringCol = new StringCollection();
      String[] arr = new String[] { "A", "B", "C", "D", "E" };
      Console.WriteLine("Array elements...");
      foreach (string res in arr) {
         Console.WriteLine(res);
      }
      stringCol.AddRange(arr);
      Console.WriteLine("Total number of elements = "+stringCol.Count);
      stringCol.RemoveAt(1);
      stringCol.RemoveAt(2);
      Console.WriteLine("Total number of elements now = "+stringCol.Count);
   }
}

出力

これにより、次の出力が生成されます-

Array elements...
A
B
C
D
E
Total number of elements = 5
Total number of elements now = 3

  1. C#のArrayListから要素の範囲を削除します

    ArrayListから要素の範囲を削除するためのコードは、次のとおりです- 例 using System; using System.Collections; public class Demo {    public static void Main(String[] args){       ArrayList list1 = new ArrayList();       list1.Add("A");       list1.Add("B"

  2. C#のArrayListからすべての要素を削除します

    ArrayListからすべての要素を削除するには、コードは次のとおりです- 例 using System; using System.Collections; public class Demo {    public static void Main(String[] args){       ArrayList list1 = new ArrayList();       list1.Add("A");       list1.Add("B"