當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C# StringCollection用法及代碼示例

StringCollection 類是 .NET Framework 類庫中的新成員,表示字符串集合。 StringCollection 類在 System.Collections.Specialized 命名空間中定義。

特征:

  • StringCollection 類接受空值作為有效值並允許重複元素。
  • 字符串比較區分大小寫。
  • 可以使用整數索引來訪問此集合中的元素。
  • 該集合中的索引是從零開始的。

Constructors

構造函數 說明
StringCollection() 初始化 StringCollection 類的新實例。

例子:


// C# code to create a 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(); 
  
        // Adding elements in StringCollection 
        myCol.Add("A"); 
        myCol.Add("B"); 
        myCol.Add("C"); 
        myCol.Add("D"); 
        myCol.Add("E"); 
  
        // Displaying objects in myCol 
        foreach(Object obj in myCol) 
        { 
            Console.WriteLine(obj); 
        } 
    } 
} 

輸出:

A
B
C
D
E

Properties

屬性 說明
Count 獲取 StringCollection 中包含的字符串數。
IsReadOnly 獲取一個值,該值指示StringCollection是否為隻讀。
IsSynchronized 獲取一個值,該值指示對 StringCollection 的訪問是否同步(線程安全)。
項目[Int32] 獲取或設置指定索引處的元素。
SyncRoot 獲取可用於同步對 StringCollection 的訪問的對象。

例子:


// C# code to illustrate the StringCollection 
// Class Properties 
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); 
  
        // --------- Using IsReadOnly Property --------- 
          
        // checking if StringCollection is 
        // read-only 
        Console.WriteLine(myCol.IsReadOnly); 
          
        // --------- Using Count Property --------- 
          
        // To get number of Strings contained 
        // in the StringCollection 
        Console.WriteLine("Number of strings in myCol are : " 
                                              + myCol.Count); 
    } 
} 

輸出:

False
Number of strings in myCol are : 5

Methods

方法 說明
Add(String) 將字符串添加到 StringCollection 的末尾。
添加範圍(字符串[]) 將字符串數組的元素複製到 StringCollection 的末尾。
Clear() 從 StringCollection 中刪除所有字符串。
Contains(String) 確定指定的字符串是否在 StringCollection 中。
複製到(字符串[],Int32) 從目標數組的指定索引開始,將整個 StringCollection 值複製到一維字符串數組。
Equals(Object) 確定指定對象是否等於當前對象。
GetEnumerator() 返回遍曆 StringCollection 的 StringEnumerator。
GetHashCode() 用作默認的哈希函數。
GetType() 獲取當前實例的類型。
IndexOf(String) 搜索指定的字符串並返回 StringCollection 中第一次出現的從零開始的索引。
插入(Int32,字符串) 將字符串插入 StringCollection 中的指定索引處。
MemberwiseClone() 創建當前對象的淺拷貝。
Remove(String) 從 StringCollection 中刪除第一次出現的特定字符串。
刪除 (Int32) 刪除 StringCollection 的指定索引處的字符串。
ToString() 返回表示當前對象的字符串。

例子:


// C# code to copy StringCollection to array, 
// starting at the specified index of 
// the target array 
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 myArr1 
        String[] myArr1 = new String[] { "A", "B", "C", "D", "E" }; 
  
        // Copying the elements of a string 
        // array to the end of the StringCollection. 
        myCol.AddRange(myArr1); 
  
        // creating a String array named myArr2 
        String[] myArr2 = new String[myCol.Count]; 
  
        // Copying StringCollection to array myArr2 
        // starting from index 0 
        myCol.CopyTo(myArr2, 0); 
  
        // Displaying elements in array myArr2 
        for (int i = 0; i < myArr2.Length; i++) { 
            Console.WriteLine(myArr2[i]); 
        } 
    } 
} 

輸出:

A
B
C
D
E

例子:


// C# code to insert a string into 
// the StringCollection at the 
// specified index 
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[] { "Hello", "Geeks", 
                                        "for", "GeeksforGeeks" }; 
  
        // Copying the elements of a string 
        // array to the end of the StringCollection. 
        myCol.AddRange(myArr); 
  
        Console.WriteLine("Initially elements in StringCollection are: "); 
  
        // Displaying elements in StringCollection 
        // named myCol 
        foreach(Object obj in myCol) 
            Console.WriteLine(obj); 
  
        // Removing all the elements from StringCollection 
        myCol.Clear(); 
  
        Console.WriteLine("After Removing: "); 
  
        // Displaying elements in StringCollection 
        // named myCol 
        foreach(Object obj in myCol) 
            Console.WriteLine(obj); 
    } 
} 

輸出:

Initially elements in StringCollection are: 
Hello
Geeks
for
GeeksforGeeks
After Removing:

參考:



相關用法


注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 C# | StringCollection Class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。