
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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