C# 中的 StringCollection 类

WBOY
WBOY 转载
2023-09-06 18:49:02 829浏览

C# 中的 StringCollection 类

StringCollection 类表示字符串的集合。以下是 StringCollection 类的属性 -

Sr.no
属性及说明
1Count

获取包含的键/值对的数量 OrderedDictionary 集合。

2IsReadOnly

获取一个值,指示 StringCollection 是否为 只读..

3IsSynchronized strong>

获取一个值,指示是否访问 StringCollection 是同步的(线程安全)。

4Item[Int32]

获取或设置指定索引处的元素。

5SyncRoot

获取可用于同步对 StringCollection 的访问的对象。

以下是 StringCollection 类的方法 -

老师编号方法及说明
1Add(String)

将字符串添加到末尾StringCollection。

2AddRange(String[] )

将字符串数组的元素复制到末尾 StringCollection。

3Clear() strong>

从 StringCollection 中删除所有字符串。

4Contains(String)

判断指定字符串是否在 StringCollection。

5CopyTo(String[] ,Int32)

将整个 StringCollection 值复制到一维字符串数组,从指定位置开始 目标数组的索引。

6Equals( Object)

判断指定对象是否等于 当前对象。 (继承自Object)

7GetEnumerator()

返回一个迭代的 StringEnumerator StringCollection。

现在让我们看一些示例

检查两个 StringCollection 对象是否相等,代码如下 -

示例

现场演示

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

输出

这将产生以下输出 −

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

要检查指定的字符串是否在StringCollection中,代码如下 −

示例

在线演示

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

输出

这将产生以下输出 −

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

以上就是C# 中的 StringCollection 类的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除