C# | StringCollection Class
StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. StringCollection class is defined in the System.Collections.Specialized namespace.
Characteristics:
- StringCollection class accepts null as a valid value and allows duplicate elements.
- String comparisons are case-sensitive.
- Elements in this collection can be accessed using an integer index.
- Indexes in this collection are zero-based.
Constructors
| Constructor | Description |
|---|---|
| StringCollection() | Initializes a new instance of the StringCollection class. |
Example:
// C# code to create a StringCollectionusing 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); } }} |
Output:
A B C D E
Properties
| Property | Description |
|---|---|
| Count | Gets the number of strings contained in the StringCollection. |
| IsReadOnly | Gets a value indicating whether the StringCollection is read-only. |
| IsSynchronized | Gets a value indicating whether access to the StringCollection is synchronized (thread safe). |
| Item[Int32] | Gets or sets the element at the specified index. |
| SyncRoot | Gets an object that can be used to synchronize access to the StringCollection. |
Example:
// C# code to illustrate the StringCollection// Class Propertiesusing 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); }} |
Output:
False Number of strings in myCol are : 5
Methods
| Method | Description |
|---|---|
| Add(String) | Adds a string to the end of the StringCollection. |
| AddRange(String[]) | Copies the elements of a string array to the end of the StringCollection. |
| Clear() | Removes all the strings from the StringCollection. |
| Contains(String) | Determines whether the specified string is in the StringCollection. |
| CopyTo(String[], Int32) | Copies the entire StringCollection values to a one-dimensional array of strings, starting at the specified index of the target array. |
| Equals(Object) | Determines whether the specified object is equal to the current object. |
| GetEnumerator() | Returns a StringEnumerator that iterates through the StringCollection. |
| GetHashCode() | Serves as the default hash function. |
| GetType() | Gets the Type of the current instance. |
| IndexOf(String) | Searches for the specified string and returns the zero-based index of the first occurrence within the StringCollection. |
| Insert(Int32, String) | Inserts a string into the StringCollection at the specified index. |
| MemberwiseClone() | Creates a shallow copy of the current Object. |
| Remove(String) | Removes the first occurrence of a specific string from the StringCollection. |
| RemoveAt(Int32) | Removes the string at the specified index of the StringCollection. |
| ToString() | Returns a string that represents the current object. |
Example:
// C# code to copy StringCollection to array,// starting at the specified index of// the target arrayusing 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]); } }} |
Output:
A B C D E
Example:
// C# code to insert a string into// the StringCollection at the// specified indexusing 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); }} |
Output:
Initially elements in StringCollection are: Hello Geeks for GeeksforGeeks After Removing:
Reference:


