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
How to create a StringCollection in C#?
The StringCollection class in C# is a specialized collection that stores only string values. It is part of the System.Collections.Specialized namespace and provides methods to add, remove, and manipulate strings efficiently.
StringCollection is useful when you need to work exclusively with string data and want type safety without the overhead of generic collections.
Syntax
Following is the syntax for creating a StringCollection −
StringCollection collection = new StringCollection();
To add multiple strings at once using an array −
string[] array = {"string1", "string2", "string3"};
collection.AddRange(array);
Creating and Populating a StringCollection
Using AddRange() Method
The most efficient way to populate a StringCollection is using the AddRange() method with a string array −
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
StringCollection strCol = new StringCollection();
String[] strArr = new String[] { "A", "B", "C", "D", "E", "F", "G", "H" };
Console.WriteLine("Original array elements...");
foreach (string str in strArr){
Console.WriteLine(str);
}
strCol.AddRange(strArr);
Console.WriteLine("Element at 5th index = " + strCol[5]);
Console.WriteLine("Count of strings in StringCollection = " + strCol.Count);
}
}
The output of the above code is −
Original array elements... A B C D E F G H Element at 5th index = F Count of strings in StringCollection = 8
Using Add() and Remove() Methods
Adding Individual Elements
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
StringCollection stringCol = new StringCollection();
// Adding individual elements
stringCol.Add("Apple");
stringCol.Add("Banana");
stringCol.Add("Cherry");
stringCol.Add("Apple"); // Duplicates allowed
Console.WriteLine("Elements in StringCollection:");
foreach (string item in stringCol){
Console.WriteLine(item);
}
Console.WriteLine("Total elements: " + stringCol.Count);
// Remove first occurrence of "Apple"
stringCol.Remove("Apple");
Console.WriteLine("After removing 'Apple': " + stringCol.Count);
}
}
The output of the above code is −
Elements in StringCollection: Apple Banana Cherry Apple Total elements: 4 After removing 'Apple': 3
Working with Numeric Strings
Example with Remove Operation
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
StringCollection stringCol = new StringCollection();
String[] arr = new String[] { "100", "200", "100", "400", "500" };
Console.WriteLine("Array elements...");
foreach (string res in arr){
Console.WriteLine(res);
}
stringCol.AddRange(arr);
Console.WriteLine("Total number of elements = " + stringCol.Count);
// Remove only removes the first occurrence
stringCol.Remove("100");
Console.WriteLine("Total number of elements after removing '100' = " + stringCol.Count);
Console.WriteLine("Remaining elements:");
foreach (string item in stringCol){
Console.WriteLine(item);
}
}
}
The output of the above code is −
Array elements... 100 200 100 400 500 Total number of elements = 5 Total number of elements after removing '100' = 4 Remaining elements: 200 100 400 500
Key Features
| Feature | Description |
|---|---|
| Type Safety | Only accepts string values |
| Duplicates | Allows duplicate string values |
| Indexing | Supports zero-based indexing with [index] |
| Case Sensitivity | String comparisons are case-sensitive |
Conclusion
StringCollection provides a simple, type-safe way to work with collections of strings in C#. It supports common operations like adding, removing, and indexing elements. While it allows duplicates and maintains insertion order, modern applications often prefer List<string> for better performance and more features.
