System.Configuration.CommaDelimitedStringCollection 类

方法描述

表示以逗号分隔的字符串元素的集合。 此类不能被继承。

语法定义(C# System.Configuration.CommaDelimitedStringCollection 类 的用法)

public sealed class CommaDelimitedStringCollection : StringCollection

构造函数

构造函数名称 构造函数描述
CommaDelimitedStringCollection 创建 CommaDelimitedStringCollection 类的新实例。

成员/方法

方法名称 方法描述
Add 向以逗号分隔的集合添加一个字符串。
AddRange 将字符串数组中的所有字符串添加到集合中。
Clear 清除集合。
Clone 创建集合的一个副本。
Contains 确定指定的字符串是否在 StringCollection 中。 (继承自 StringCollection。)
CopyTo 从目标数组的指定索引处开始,将全部 StringCollection 值复制到一维字符串数组中。 (继承自 StringCollection。)
Equals(Object) 确定指定的 Object 是否等于当前的 Object。 (继承自 Object。)
Finalize 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
GetEnumerator 返回循环访问 StringCollection 的 StringEnumerator。 (继承自 StringCollection。)
GetHashCode 用作特定类型的哈希函数。 (继承自 Object。)
GetType 获取当前实例的 Type。 (继承自 Object。)
IndexOf 搜索指定的字符串并返回 StringCollection 内的第一个匹配项的从零开始的索引。 (继承自 StringCollection。)
Insert 将一个字符串元素添加到集合中的指定索引位置。
MemberwiseClone 创建当前 Object 的浅表副本。 (继承自 Object。)
Remove 从集合中移除一个字符串元素。
RemoveAt 移除 StringCollection 的指定索引处的字符串。 (继承自 StringCollection。)
SetReadOnly 将集合对象设置为只读。
ToString 返回对象的字符串表示形式。 (重写 Object.ToString()。)

提示和注释

此类表示序列化为以逗号分隔的字符串元素列表的字符串集合。

System.Configuration.CommaDelimitedStringCollection 类例子

下面的代码示例演示如何使用 CommaDelimitedStringCollection 类型。

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Collections.Specialized;

namespace Samples.AspNet.Config
{
  class CommaDelimitedStrCollection
  {
    static void Main(string[] args)
    {
      // Display title and info.
      Console.WriteLine("ASP.NET Configuration Info");
      Console.WriteLine("Type: CommaDelimitedStringCollection");
      Console.WriteLine();

      // Set the path of the config file.
      string configPath = "/aspnet";

      // Get the Web application configuration object.
      Configuration config = 
        WebConfigurationManager.OpenWebConfiguration(configPath);

      // Get the section related object.
      AuthorizationSection configSection =
        (AuthorizationSection)config.GetSection("system.web/authorization");

      // Get the authorization rule collection.
      AuthorizationRuleCollection authorizationRuleCollection = 
        configSection.Rules;

      // Create a CommaDelimitedStringCollection object.
      CommaDelimitedStringCollection myStrCollection =
        new CommaDelimitedStringCollection();

      for (int i = 0; i < authorizationRuleCollection.Count; i++)
      {
        if (authorizationRuleCollection.Get(i).Action.ToString().ToLower() 
          == "allow")
        {
          // Add values to the CommaDelimitedStringCollection object.
          myStrCollection.AddRange(
            authorizationRuleCollection.Get(i).Users.ToString().Split(
            ",".ToCharArray()));
        }
      }

      Console.WriteLine("Allowed Users: {0}",
        myStrCollection.ToString());

      // Count the elements in the collection.
      Console.WriteLine("Allowed User Count: {0}", 
        myStrCollection.Count);

      // Call the Contains method.
      Console.WriteLine("Contains 'userName1': {0}",
        myStrCollection.Contains("userName1"));

      // Determine the index of an element
      // in the collection.
      Console.WriteLine("IndexOf 'userName0': {0}",
        myStrCollection.IndexOf("userName0"));

      // Call IsModified.
      Console.WriteLine("IsModified: {0}",
        myStrCollection.IsModified);

      // Call IsReadyOnly.
      Console.WriteLine("IsReadOnly: {0}",
        myStrCollection.IsReadOnly);

      Console.WriteLine();
      Console.WriteLine("Add a user name to the collection.");
      // Insert a new element in the collection.
      myStrCollection.Insert(myStrCollection.Count, "userNameX");

      Console.WriteLine("Collection Value: {0}",
        myStrCollection.ToString());

      Console.WriteLine();
      Console.WriteLine("Remove a user name from the collection.");
      // Remove an element of the collection.
      myStrCollection.Remove("userNameX");

      Console.WriteLine("Collection Value: {0}",
        myStrCollection.ToString());

      // Display and wait
      Console.ReadLine();
    }
  }
}

继承层次结构

System.Object

System.Collections.Specialized.StringCollection

System.Configuration.CommaDelimitedStringCollection

命名空间

namespace: System.Configuration

程序集: System.Configuration(在 System.Configuration.dll 中)

线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。