Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
StringCollection Class in C#
The StringCollection class in C# is a specialized collection that stores strings. It is part of the System.Collections.Specialized namespace and provides methods specifically designed for string operations, making it more efficient than generic collections when working exclusively with strings.
Properties
Following are the key properties of the StringCollection class −
| 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. |
Methods
Following are the commonly used methods of the StringCollection class −
| 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. |
| GetEnumerator() | Returns a StringEnumerator that iterates through the StringCollection. |
| Remove(String) | Removes the first occurrence of a specific string from the StringCollection. |
Using Add() and Contains() Methods
The following example demonstrates adding strings to a StringCollection and checking if a specific string exists −
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection stringCol = new StringCollection();
String[] arr = new String[] { "100", "200", "300", "400", "500" };
Console.WriteLine("Array elements...");
foreach (string res in arr) {
Console.WriteLine(res);
}
stringCol.AddRange(arr);
Console.WriteLine("Count: " + stringCol.Count);
Console.WriteLine("Does the collection contain '300'? " + stringCol.Contains("300"));
Console.WriteLine("Does the collection contain '800'? " + stringCol.Contains("800"));
}
}
The output of the above code is −
Array elements... 100 200 300 400 500 Count: 5 Does the collection contain '300'? True Does the collection contain '800'? False
Using Remove() and Clear() Methods
The following example shows how to remove specific items and clear the entire collection −
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection strCol = new StringCollection();
strCol.Add("Apple");
strCol.Add("Banana");
strCol.Add("Cherry");
strCol.Add("Date");
Console.WriteLine("Original collection:");
foreach (string fruit in strCol) {
Console.WriteLine(fruit);
}
strCol.Remove("Banana");
Console.WriteLine("\nAfter removing 'Banana':");
foreach (string fruit in strCol) {
Console.WriteLine(fruit);
}
Console.WriteLine("Count: " + strCol.Count);
strCol.Clear();
Console.WriteLine("Count after Clear(): " + strCol.Count);
}
}
The output of the above code is −
Original collection: Apple Banana Cherry Date After removing 'Banana': Apple Cherry Date Count: 3 Count after Clear(): 0
Using Index-Based Access
StringCollection supports index-based access to elements using the Item[Int32] property −
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection colors = new StringCollection();
colors.Add("Red");
colors.Add("Green");
colors.Add("Blue");
Console.WriteLine("Accessing elements by index:");
for (int i = 0; i < colors.Count; i++) {
Console.WriteLine("Index " + i + ": " + colors[i]);
}
colors[1] = "Yellow";
Console.WriteLine("\nAfter modifying index 1:");
foreach (string color in colors) {
Console.WriteLine(color);
}
}
}
The output of the above code is −
Accessing elements by index: Index 0: Red Index 1: Green Index 2: Blue After modifying index 1: Red Yellow Blue
Conclusion
The StringCollection class provides a specialized, efficient way to work with collections of strings in C#. It offers useful methods like Add(), Remove(), Contains(), and AddRange(), along with index-based access. While modern development often uses generic collections like List<string>, StringCollection remains useful for scenarios requiring specialized string collection functionality.
