C# | StringCollection Class - GeeksforGeeks

24 Jan, 2019
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.
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

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

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:
https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.stringcollection?view=netframework-4.7.2
CSharp-Collections-Namespace
CSharp-Specialized-Namespace
CSharp-Specialized-StringCollection
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Introduction to .NET Framework
C# | Constructors
Partial Classes in C#
Difference between Ref and Out keywords in C#
C# | Delegates
C# | Class and Object
Extension Method in C#
Basic CRUD (Create, Read, Update, Delete) in ASP.NET MVC Using C# and Entity Framework
Top 50 C# Interview Questions & Answers
C# | Encapsulation