StringCollection Class in C#


The StringCollection class represents a collection of strings. Following are the properties of the StringCollection class −

Sr.noProperty & Description
1Count
Gets the number of key/values pairs contained in the OrderedDictionary collection.
2IsReadOnly
Gets a value indicating whether the StringCollection is read-only..
3IsSynchronized
Gets a value indicating whether access to the StringCollection is synchronized (thread safe).
4Item[Int32]
Gets or sets the element at the specified index.
5SyncRoot
Gets an object that can be used to synchronize access to the StringCollection.

Following are the methods of the StringCollection class −

Sr.noMethod & Description
1Add(String)
Adds a string to the end of the StringCollection.
2AddRange(String[])
Copies the elements of a string array to the end of the StringCollection.
3Clear()
Removes all the strings from the StringCollection.
4Contains(String)
Determines whether the specified string is in the StringCollection.
5CopyTo(String[],Int32)
Copies the entire StringCollection values to a onedimensional array of strings, starting at the specified index of the target array.
6Equals(Object)
Determines whether the specified object is equal to the current object. (Inherited from Object)
7GetEnumerator()
Returns a StringEnumerator that iterates through the StringCollection.

Let us now see some examples

To check if two StringCollection objects are equal, the code is as follows −

Example

 Live Demo

using System;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      StringCollection strCol1 = new StringCollection();
      strCol1.Add("Accessories");
      strCol1.Add("Books");
      strCol1.Add("Electronics");
      Console.WriteLine("StringCollection1 elements...");
      foreach (string res in strCol1) {
         Console.WriteLine(res);
      }
      StringCollection strCol2 = new StringCollection();
      strCol2.Add("Accessories");
      strCol2.Add("Books");
      strCol2.Add("Electronics");
      Console.WriteLine("StringCollection2 elements...");
      foreach (string res in strCol1) {
         Console.WriteLine(res);
      }
      Console.WriteLine("Both the String Collections are equal? = "+strCol1.Equals(strCol2));
   }
}

Output

This will produce the following output −

StringCollection1 elements...
Accessories
Books
Electronics
StringCollection2 elements...
Accessories
Books
Electronics
Both the String Collections are equal? = False

To check if the specified string is in the StringCollection, the code is as follows −

Example

 Live Demo

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("Does the specified string is in the StringCollection? = "+stringCol.Contains("800"));
   }
}

Output

This will produce the following output −

Array elements...
100
200
300
400
500
Does the specified string is in the StringCollection? = False

Updated on: 10-Dec-2019

516 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements