C# StringCollection Class (original) (raw)

Last Updated : 04 Feb, 2025

In C#, the **StringCollection class is part of the System.Collections.Specialized namespace. It provides a collection of strings that allows us to store and manage a list of strings easily. This class is a type-safe collection that automatically resizes as needed.

**Example: This example demonstrates how to add a string to a StringCollection and iterate over the collection to display its elements.

C# `

// C# code to create a StringCollection using System; using System.Collections; using System.Collections.Specialized;

class Geeks {

public static void Main() 
{ 
    StringCollection s = new StringCollection(); 

    // Adding elements in StringCollection 
    s.Add("Geeks"); 
    s.Add("for"); 
    s.Add("Geeks"); 

    foreach(Object i in s) 
    { 
        Console.WriteLine(i); 
    } 
} 

}

`

Declaration of StringCollection

In C#, the declaration of StringCollection is as follow:

StringCollection collection = new StringCollection();

Constructor

**StringCollection(): Initializes a new instance of the StringCollection class.

**Example: This example demonstrates the working of StringCollection.

C# `

// Example of StringCollection using System; using System.Collections; using System.Collections.Specialized;

class Geeks {

public static void Main() 
{ 

    StringCollection s = new StringCollection(); 

    // Adding elements in StringCollection 
    s.Add("A"); 
    s.Add("B"); 
    s.Add("C");


    // Displaying objects 
    foreach(Object i in s) 
    { 
        Console.WriteLine(i); 
    } 
} 

}

`

Properties

The StringCollection class provides several properties to access its state.

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: This example demonstrates how to use **StringColelction class, including the AddRange method to add multiple elements, the **IsReadOnly property to check if the collection is ready to read-only and the count property to get the number of elements in the collection.

C# `

// C# code to demonstrates the StringCollection // Class Properties using System; using System.Collections; using System.Collections.Specialized;

class Geeks {

public static void Main() 
{ 

    // creating a StringCollection 
    StringCollection s = new StringCollection(); 

    // creating a string array 
    String[] arr = new String[] { "A", "B", "C", "D", "E" }; 

    // Copying the elements of a string 
    // array to the end of the StringCollection
    s.AddRange(arr); 

    // IsReadOnly Property
    
    // checking if StringCollection is 
    // read-only 
    Console.WriteLine(s.IsReadOnly); 
    
    // Using Count Property
    
    // To get number of Strings contained 
    // in the StringCollection 
    Console.WriteLine("Number of strings are: "
                                        + s.Count); 
} 

}

`

Output

False Number of strings 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 1: This example demonstrates how to use **AddRange and **CopyTo method of the StringCollection class to copy elements from a collection to a string array.

C# `

// 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 Geeks {

public static void Main()
{
    // creating a StringCollection named s 
    StringCollection s = new StringCollection();

    // creating a string array named arr
    String[] arr = new String[] { "A", "B", "C", "D", "E" };

    // Copying the elements of a string 
    // array to the end of the StringCollection 
    s.AddRange(arr);

    // creating a String array named arr2 
    String[] arr2 = new String[s.Count];

    // Copying StringCollection to array arr2 
    // starting from index 0 
    s.CopyTo(arr2, 0);

    // Displaying elements in array arr2 
    for (int i = 0; i < arr2.Length; i++) {
        Console.WriteLine(arr2[i]);
    }
}

}

`

**Example 2: This example demonstrates how to add elements to a StringCollection using AddRange, display its elements and then remove all elements using the clear() method.

C# `

// C# code to insert a string into // the StringCollection at the // specified index using System; using System.Collections; using System.Collections.Specialized;

class Geeks {

public static void Main()
{
    // creating a StringCollection named s 
    StringCollection s = new StringCollection();

    // creating a string array named arr
    String[] arr = new String[] { "A", "B",
            "C", "D" };

    // Copying the elements of a string 
    // array to the end of the StringCollection
    s.AddRange(arr);

    Console.WriteLine("Initially elements in StringCollection are: ");

    // Displaying elements in StringCollection  
    foreach(Object i in s)
    Console.WriteLine(i);

    // Removing all the elements from StringCollection 
    s.Clear();

    Console.WriteLine("After Removing: ");

    // Displaying elements in StringCollection 
    foreach(Object i in s)
    Console.WriteLine(i);
}

}

`

Output

Initially elements in StringCollection are: A B C D After Removing: