StringCollection class in C#

WBOY
Release: 2023-09-06 18:49:02
forward
1255 people have browsed it

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));
   }
}
Copy after login

Output

This will produce the following output−

StringCollection1 elements...
Accessories
Books
Electronics
StringCollection2 elements...
Accessories
Books
Electronics
Both the String Collections are equal? = False
Copy after login

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

Output

This will produce the following output−

Array elements...
100
200
300
400
500
Does the specified string is in the StringCollection? = False
Copy after login

The above is the detailed content of StringCollection class in C#. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template