Home > Article > Backend Development > StringCollection class in C#
The StringCollection class represents a collection of strings. The following are the properties of the StringCollection class-
Properties and description | |
---|---|
1 |
Count Get the number of key/value pairs contained OrderedDictionary collection. |
2 |
IsReadOnly Gets a value indicating whether the StringCollection is Read only.. |
3 | IsSynchronized strong> Gets a value indicating whether to access 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 a StringCollection. |
The following are the methods of StringCollection class-
Teacher ID | Method and description |
---|---|
1 |
Add(String) Add the string to End StringCollection. |
2 |
AddRange(String[] ) Copy the elements of the string array to the end StringCollection. |
3 | Clear() strong> Removes all strings from the StringCollection. |
4 |
Contains(String) Determine whether the specified string is in StringCollection. |
5 |
CopyTo(String[] ,Int32) Copy the entire StringCollection value to a one-dimensional string array, starting at the specified position The index of the target array. |
6 |
Equals(Object) Determine whether the specified object is equal to current object. (Inherited from Object) |
7 |
GetEnumerator() Returns an iterated StringEnumerator StringCollection. |
Now let us see some examples
To check whether two StringCollection objects are equal or not, the code is as follows-
Live demonstration
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)); } }
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 whether the specified string is in the StringCollection, the code is as follows−
Online 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")); } }
This will produce the following output−
Array elements... 100 200 300 400 500 Does the specified string is in the StringCollection? = False
The above is the detailed content of StringCollection class in C#. For more information, please follow other related articles on the PHP Chinese website!