Home  >  Article  >  Backend Development  >  StringCollection class in C#

StringCollection class in C#

WBOY
WBOYforward
2023-09-06 18:49:021235browse

C# 中的 StringCollection 类

The StringCollection class represents a collection of strings. The following are the properties of the StringCollection class-

Sr.no
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-

Example

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));
   }
}

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 whether the specified string is in the StringCollection, the code is as follows−

Example

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"));
   }
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete