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 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); } } } |
chevron_right
filter_none
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 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); } } |
chevron_right
filter_none
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 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]); } } } |
chevron_right
filter_none
Output:
A B C D E
Example:
// 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); } } |
chevron_right
filter_none
Output:
Initially elements in StringCollection are: Hello Geeks for GeeksforGeeks After Removing:
Reference:
Recommended Posts:
- C# | Get or Set at specified index in StringCollection
- C# | Add a string to the end of the StringCollection
- How to create a StringCollection in C#
- C# | Check if the StringCollection is read-only
- How to get Synchronize access to the StringCollection in C#
- C# | Index of first occurrence in StringCollection
- C# | Remove from the specified index of the StringCollection
- C# | Insert at the specified index in StringCollection
- C# | Remove the first occurrence from the StringCollection
- C# | Get an enumerator that iterates through StringCollection
- C# | Check if the specified string is in the StringCollection
- C# | Get the number of strings in StringCollection
- C# | Remove all the strings from the StringCollection
- C# | Check if two StringCollection objects are equal
- C# | Gets or sets the element at the specified index in StringCollection
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



