Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
C# String Class
Next article icon

C# StringCollection Class

Last Updated : 04 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C#, the StringCollection class is part of the System.Collections.Specialized namespace. It provides a collection of strings that allows us to store and manage a list of strings easily. This class is a type-safe collection that automatically resizes as needed.

  • 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.

Example: This example demonstrates how to add a string to a StringCollection and iterate over the collection to display its elements.

C#
// C# code to create a StringCollection  using System;  using System.Collections;  using System.Collections.Specialized;   class Geeks {   	public static void Main()  	{  		StringCollection s = new StringCollection();   		// Adding elements in StringCollection  		s.Add("Geeks");  		s.Add("for");  		s.Add("Geeks");  	 		foreach(Object i in s)  		{  			Console.WriteLine(i);  		}  	}  }  

Output
Geeks for Geeks 

Declaration of StringCollection

In C#, the declaration of StringCollection is as follow:

StringCollection collection = new StringCollection();

Constructor

StringCollection(): Initializes a new instance of the StringCollection class.

Example: This example demonstrates the working of StringCollection.

C#
// Example of StringCollection using System;  using System.Collections;  using System.Collections.Specialized;   class Geeks {   	public static void Main()  	{   		StringCollection s = new StringCollection();   		// Adding elements in StringCollection  		s.Add("A");  		s.Add("B");  		s.Add("C"); 	  		// Displaying objects  		foreach(Object i in s)  		{  			Console.WriteLine(i);  		}  	}  }  

Output
A B C 

Properties

The StringCollection class provides several properties to access its state.

PropertyDescription
CountGets the number of strings contained in the StringCollection.
IsReadOnlyGets a value indicating whether the StringCollection is read-only.
IsSynchronizedGets a value indicating whether access to the StringCollection is synchronized (thread safe).
Item[Int32]Gets or sets the element at the specified index.
SyncRootGets an object that can be used to synchronize access to the StringCollection.

Example: This example demonstrates how to use StringColelction class, including the AddRange method to add multiple elements, the IsReadOnly property to check if the collection is ready to read-only and the count property to get the number of elements in the collection.

C#
// C# code to demonstrates the StringCollection  // Class Properties  using System;  using System.Collections;  using System.Collections.Specialized;   class Geeks {   	public static void Main()  	{   		// creating a StringCollection  		StringCollection s = new StringCollection();   		// creating a string array  		String[] arr = new String[] { "A", "B", "C", "D", "E" };   		// Copying the elements of a string  		// array to the end of the StringCollection 		s.AddRange(arr);   		// IsReadOnly Property 		 		// checking if StringCollection is  		// read-only  		Console.WriteLine(s.IsReadOnly);  		 		// Using Count Property 		 		// To get number of Strings contained  		// in the StringCollection  		Console.WriteLine("Number of strings are: " 											+ s.Count);  	}  }  

Output
False Number of strings are: 5 

Methods

MethodDescription
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 1: This example demonstrates how to use AddRange and CopyTo method of the StringCollection class to copy elements from a collection to a string array.

C#
// 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 Geeks {      public static void Main()     {         // creating a StringCollection named s          StringCollection s = new StringCollection();          // creating a string array named arr         String[] arr = new String[] { "A", "B", "C", "D", "E" };          // Copying the elements of a string          // array to the end of the StringCollection          s.AddRange(arr);          // creating a String array named arr2          String[] arr2 = new String[s.Count];          // Copying StringCollection to array arr2          // starting from index 0          s.CopyTo(arr2, 0);          // Displaying elements in array arr2          for (int i = 0; i < arr2.Length; i++) {             Console.WriteLine(arr2[i]);         }     } }  

Output
A B C D E 

Example 2: This example demonstrates how to add elements to a StringCollection using AddRange, display its elements and then remove all elements using the clear() method.

C#
// C# code to insert a string into  // the StringCollection at the  // specified index  using System; using System.Collections; using System.Collections.Specialized;  class Geeks {       public static void Main()     {         // creating a StringCollection named s          StringCollection s = new StringCollection();          // creating a string array named arr         String[] arr = new String[] { "A", "B",                 "C", "D" };          // Copying the elements of a string          // array to the end of the StringCollection         s.AddRange(arr);          Console.WriteLine("Initially elements in StringCollection are: ");          // Displaying elements in StringCollection           foreach(Object i in s)         Console.WriteLine(i);          // Removing all the elements from StringCollection          s.Clear();          Console.WriteLine("After Removing: ");          // Displaying elements in StringCollection          foreach(Object i in s)         Console.WriteLine(i);     } }  

Output
Initially elements in StringCollection are:  A B C D After Removing:  


Next Article
C# String Class

S

Sahil_Bansall
Improve
Article Tags :
  • C#
  • CSharp-Specialized-Namespace
  • CSharp-Specialized-StringCollection

Similar Reads

  • C# | Collection Class
    .math-table { border-collapse: collapse; width: 100%; } .math-table td { border: 1px solid #5fb962; text-align: left !important; padding: 8px; } .math-table th { border: 1px solid #5fb962; padding: 8px; } .math-table tr>th{ background-color: #c6ebd9; vertical-align: middle; } .math-table tr:nth-chil
    5 min read
  • C# StringDictionary Class
    In C#, the StringDictionary class is the part of the System.Collections.Specialized namespace. It is a collection of key-value pairs where the keys are strings and values are objects. It is similar to a Hashtable. But it is specifically designed for use with string keys. Keys are always strings, and
    5 min read
  • C# String Class
    In C#, a string is a sequence of Unicode characters or an array of characters. The range of Unicode characters will be U+0000 to U+FFFF. The array of characters is also termed as the text. So the string is the representation of the text. A string is represented by a class System.String. The String c
    9 min read
  • How to create a StringCollection in C#
    StringCollection() constructor is used to initializing a new instance of the 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
    2 min read
  • C# | Check if the StringCollection is read-only
    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. StringCollection.IsReadOnly property is used to get a value indicating whether the StringCollection
    1 min read
  • C# | Check if two StringCollection objects are equal
    Equals(Object) Method which is inherited from the Object class is used to check if a specified StringCollection object is equal to another StringCollection object or not. Syntax: public virtual bool Equals (object obj); Here, obj is the object which is to be compared with the current object. Return
    2 min read
  • C# | Insert at the specified index in StringCollection
    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. StringCollection.Insert(Int32, String) method is used to insert a string into the StringCollection
    2 min read
  • C# | Get or Set at specified index in StringCollection
    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. Properties: StringCollection accepts null as a valid value and allows duplicate elements. String co
    2 min read
  • C# | Index of first occurrence in StringCollection
    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. StringCollection.IndexOf(String) method is used to search the specified string which returns the ze
    2 min read
  • C# | Check if StringCollection is synchronized (thread safe)
    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. StringCollection.IsSynchronized property is used to get a value indicating whether access to the St
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences