StringCollection in C#

StringCollection is a collection of strings. In this article, learn how to take advantages of StringCollection methods and properties to manage a collection of strings. StringCollection class

StringCollection

StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. In this article, we will discuss how to take advantages of its methods and properties to manage a collection of strings.

StringCollection class defined in the System.Collections.Specialized namespace represents a collection of strings and provides functionality to manage the collection.

Creating StringCollection

The following code snippet creates an object of StringCollection class using the default constructor.

  1. StringCollection authorNames = new StringCollection();   
Adding Strings
 
StringCollection class provides following three methods to add strings to a string collection.

 

  • Add
  • AddRange
  • Insert

 

Add method is used to add string to a StringCollection at the end of the collection. The following code snippet adds strings to a StringCollection.

  1. // Add string using Add method  
  2. authorNames.Add("Mahesh Chand");  
  3. authorNames.Add("Mike Gold");  
  4. authorNames.Add("Praveen Kumar");  
  5. authorNames.Add("Raj Beniwal");  
AddRange method is used to add an array of strings to a StringCollection at the end of the collection. The following code snippet adds an array of strings to a StringCollection.
  1. // Add an array of string using AddRange  
  2. string[] names = new string[]{"Mahesh Chand""Mike Gold""Praveen Kumar""Raj Beniwal"};  
  3. authorNames.AddRange(names);  
Insert method is used to insert a string at the specified location of a StringCollection. The following code snippet inserts a string at the 5th position in a StringCollection. You will get out of bounds error message if the StringCollection does not have 5 items in it.
  1. // Insert an string at a specified index  
  2. authorNames.Insert(5, "New Author");  
Accessing Strings

The foreach loop statement in C# is used to iterate through a collection of objects such as integer or string.

The following code snippet creates an array of strings, adds strings to a StringCollection and later uses foreach statement to loop through the collection and display on the system console.

  1. StringCollection authorNames = new StringCollection();  
  2. string[] names = new string[]{"Mahesh Chand""Mike Gold""Praveen Kumar""Raj Beniwal"};  
  3. authorNames.AddRange(names);  
  4. foreach (string name in authorNames)  
  5. {  
  6.     Console.WriteLine(name);  
  7. }  
Removing Strings

StringCollection class provides following three methods to remove strings to a string collection.

 

  • Clear
  • Remove
  • RemoveAt

 

Clear method removes all items from a StringCollection. The following code snippet removes all items from a StringCollection.

  1. authorNames.Clear();  
Remove method removes the first occurrence of a given string from the string collection. The following code snippet removes a string from a StringCollection.
  1. authorNames.Remove("Mike Gold");  
RemoveAt method removes a string specified at the given location from the string collection. The following code snippet removes a string at the specified index from a StringCollection.
  1. authorNames.RemoveAt(5);  
Find String

IndexOf method searches for the specified string and returns the zero-based index of the first occurrence within the StringCollection. The following code snippet finds the position of a string in a string collection.

  1. int authorLocation = authorNames.IndexOf("Mike Gold");  
  2. Console.WriteLine("Position of Mike Gold is " + authorLocation.ToString());  
Contains method returns true if a string is found in a StringCollection. The following code snippet checks if a string is found in the collection and returns the position in a string collection.
  1. if (authorNames.Contains("Mike Gold"))  
  2. {  
  3.     Console.WriteLine("Mike Gold is at position: " + authorNames.IndexOf("Mike Gold"));  
  4. }  
Copy Strings

CopyTo method of StringCollection is used to copy items from a StringCollection to an array. The CopyTo method takes two arguments. First is the name of the StringCollection and second is the starting position in the StringCollection. The following code snippet copies all items from authorNames StringCollection to an array.

  1. // Copy Collection to new Array  
  2. string[] newAuthorList = new string[authorNames.Count];  
  3. authorNames.CopyTo(newAuthorList, 0);  
  4. foreach (string name in newAuthorList)  
  5. {  
  6.     Console.WriteLine(name);  
  7. }  
Count Strings

Count property returns total number of items in in a StringCollection. The following code snippet returns number of items in authorNames collection.

  1. Console.WriteLine("Total items in string collection: " + authorNames.Count.ToString());  
Getting Items

ArrayCollection is a collection. That means, you can access its items by using an index. The following code snippet looks for the position of a string and accesses it using Item property.

  1. int authorLocation = authorNames.IndexOf("Mike Gold");  
  2. string authorName = authorNames[authorLocation];  
Complete Code

Here is the listing of complete code we have discussed in this article.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Collections;  
  6. using System.Collections.Specialized;  
  7.   
  8. namespace StringCollectionSample  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             // Create a StringCollection object    
  15.             StringCollection authorNames = new StringCollection();  
  16.             // Add string using Add method    
  17.             authorNames.Add("Mahesh Chand");  
  18.             authorNames.Add("Mike Gold");  
  19.             authorNames.Add("Praveen Kumar");  
  20.             authorNames.Add("Raj Beniwal");  
  21.             // Add an array of string using AddRange               
  22.             string[] names = new string[] { "Chris Love""Henry He""Allen Neill""David Maccarter" };  
  23.             authorNames.AddRange(names);  
  24.             // Insert an string at a specified index    
  25.             authorNames.Insert(5, "New Author");  
  26.             // authorNames.Clear();    
  27.             // authorNames.Remove("Mike Gold");    
  28.             // authorNames.RemoveAt(5);    
  29.             if (authorNames.Contains("Mike Gold"))  
  30.             {  
  31.                 Console.WriteLine("Mike Gold is at position: " + authorNames.IndexOf("Mike Gold"));  
  32.             }  
  33.             int authorLocation = authorNames.IndexOf("Mike Gold");  
  34.             string authorName = authorNames[authorLocation];  
  35.             Console.WriteLine("Position of Mike Gold is " + authorLocation.ToString());  
  36.             Console.WriteLine("Total items in string collection: " + authorNames.Count.ToString());  
  37.             Console.WriteLine("-----------------------------");  
  38.             foreach (string name in authorNames)  
  39.             {  
  40.                 Console.WriteLine(name);  
  41.             }  
  42.             Console.WriteLine("-----------------------------");  
  43.             // Copy Collection to new Array    
  44.             string[] newAuthorList = new string[authorNames.Count];  
  45.             authorNames.CopyTo(newAuthorList, 0);  
  46.             foreach (string name in newAuthorList)  
  47.             {  
  48.                 Console.WriteLine(name);  
  49.             }  
  50.             Console.ReadLine();  
  51.         }  
  52.     }  
  53. }  
The ouput from above code is shown in below figure.
 
StringCollection c# 
 
Summary

StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. In this article, we saw how to take advantages of this class and its methods and properties to manage a collection of strings.