
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
StringCollection Class in C#
The StringCollection class represents a collection of strings. Following are the properties of the StringCollection class −
Sr.no | Property & Description |
---|---|
1 | Count Gets the number of key/values pairs contained in the OrderedDictionary collection. |
2 | IsReadOnly Gets a value indicating whether the StringCollection is read-only.. |
3 | IsSynchronized Gets a value indicating whether access to the StringCollection is synchronized (thread safe). |
4 | Item[Int32] Gets or sets the element at the specified index. |
5 | SyncRoot Gets an object that can be used to synchronize access to the StringCollection. |
Following are the methods of the StringCollection class −
Sr.no | Method & Description |
---|---|
1 | Add(String) Adds a string to the end of the StringCollection. |
2 | AddRange(String[]) Copies the elements of a string array to the end of the StringCollection. |
3 | Clear() Removes all the strings from the StringCollection. |
4 | Contains(String) Determines whether the specified string is in the StringCollection. |
5 | CopyTo(String[],Int32) Copies the entire StringCollection values to a onedimensional array of strings, starting at the specified index of the target array. |
6 | Equals(Object) Determines whether the specified object is equal to the current object. (Inherited from Object) |
7 | GetEnumerator() 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
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
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
- Related Articles
- How to create a StringCollection in C#?
- Index of first occurrence in StringCollection in C#?
- Insert at the specified index in StringCollection in C#
- Get the number of strings in StringCollection in C#
- Remove all the strings from the StringCollection in C#
- Get an enumerator that iterates through StringCollection in C#
- Check if two StringCollection objects are equal in C#
- Check if the StringCollection is read-only in C#
- Remove the first occurrence from the StringCollection in C#
- Get or Set at specified index in StringCollection in C#
- Copy StringCollection at the specified index of array in C#
- How to get Synchronize access to the StringCollection in C#
- Remove from the specified index of the StringCollection in C#
- Check if the specified string is in the StringCollection in C#
- Add a string to the end of the StringCollection in C#

Advertisements