
StringCollection 类表示字符串的集合。以下是 StringCollection 类的属性 -
| 属性及说明 | |
|---|---|
| 1 | 
Count 获取包含的键/值对的数量 OrderedDictionary 集合。  | 
| 2 | 
IsReadOnly 获取一个值,指示 StringCollection 是否为 只读..  | 
| 3 | IsSynchronized strong> 获取一个值,指示是否访问 StringCollection 是同步的(线程安全)。  | 
| 4 | 
Item[Int32] 获取或设置指定索引处的元素。  | 
| 5 | 
SyncRoot 获取可用于同步对 StringCollection 的访问的对象。  | 
以下是 StringCollection 类的方法 -
| 老师编号 | 方法及说明 | 
|---|---|
| 1 | 
Add(String) 将字符串添加到末尾StringCollection。  | 
| 2 | 
AddRange(String[] ) 将字符串数组的元素复制到末尾 StringCollection。  | 
| 3 | Clear() strong> 从 StringCollection 中删除所有字符串。  | 
| 4 | 
Contains(String) 判断指定字符串是否在 StringCollection。  | 
| 5 | 
CopyTo(String[] ,Int32) 将整个 StringCollection 值复制到一维字符串数组,从指定位置开始 目标数组的索引。  | 
| 6 | 
Equals( Object) 判断指定对象是否等于 当前对象。 (继承自Object)  | 
| 7 | 
GetEnumerator() 返回一个迭代的 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中文网其它相关文章!
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号