📌  相关文章
📜  C#|从StringCollection的指定索引中删除

📅  最后修改于: 2021-05-30 00:56:45             🧑  作者: Mango

StringCollection类是.NET Framework类库的新添加,它表示字符串的集合。 StringCollection类在System.Collections.Specialized命名空间中定义。
StringCollection.RemoveAt(Int32)方法用于从StringCollection的指定索引处删除字符串。

句法:

public void RemoveAt (int index);

在这里, index是要删除的字符串的从零开始的索引。

异常:如果索引小于零索引等于或大于Count,则此方法将提供ArgumentOutOfRangeException

注意:此方法是O(n)运算,其中n是Count。

下面的程序说明StringCollection.RemoveAt(Int32)方法的用法:

范例1:

// C# code to remove the string at the
// specified index of the StringCollection
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // creating a StringCollection named myCol
        StringCollection myCol = new StringCollection();
  
        // creating a string array named myArr
        String[] myArr = new String[] { "A", "B", "C", "D", "E" };
  
        // Copying the elements of a string
        // array to the end of the StringCollection.
        myCol.AddRange(myArr);
  
        Console.WriteLine("Elements in StringCollection myCol are : ");
  
        // Displaying elements in StringCollection
        // named myCol
        foreach(Object obj in myCol)
            Console.WriteLine(obj);
  
        // Removing element at index 3
        myCol.RemoveAt(3);
  
        Console.WriteLine("Elements in StringCollection myCol are : ");
  
        // Displaying elements in StringCollection
        // named myCol
        foreach(Object obj in myCol)
            Console.WriteLine(obj);
    }
}
输出:
Elements in StringCollection myCol are : 
A
B
C
D
E
Elements in StringCollection myCol are : 
A
B
C
E

范例2:

// C# code to remove the string at the
// specified index of the StringCollection
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // creating a StringCollection named myCol
        StringCollection myCol = new StringCollection();
  
        // creating a string array named myArr
        String[] myArr = new String[] { "1", "2", "3", "4", "5" };
  
        // Copying the elements of a string
        // array to the end of the StringCollection.
        myCol.AddRange(myArr);
  
        Console.WriteLine("Elements in StringCollection myCol are : ");
  
        // Displaying elements in StringCollection
        // named myCol
        foreach(Object obj in myCol)
            Console.WriteLine(obj);
  
        // Removing element at index -5
        // It should raise "ArgumentOutOfRangeException"
        // as index is less than 0
        myCol.RemoveAt(-5);
  
        Console.WriteLine("Elements in StringCollection myCol are : ");
  
        // Displaying elements in StringCollection
        // named myCol
        foreach(Object obj in myCol)
            Console.WriteLine(obj);
    }
}

输出:

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.specialized.stringcollection.removeat?view=netframework-4.7.2