CommaDelimitedStringCollection 類別

定義

表示以逗號分隔字串項目的集合。 此類別無法獲得繼承。

public ref class CommaDelimitedStringCollection sealed : System::Collections::Specialized::StringCollection
public sealed class CommaDelimitedStringCollection : System.Collections.Specialized.StringCollection
type CommaDelimitedStringCollection = class
    inherit StringCollection
Public NotInheritable Class CommaDelimitedStringCollection
Inherits StringCollection
繼承
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();
    }
  }
}
Imports System.Collections.Generic
Imports System.Text
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Configuration
Imports System.Collections.Specialized

Namespace Samples.AspNet.Config
  Class CommaDelimitedStrCollection
    Shared Sub Main(ByVal args() As String)
      ' Display title and info.
      Console.WriteLine("ASP.NET Configuration Info")
      Console.WriteLine("Type: CommaDelimitedStringCollection")
      Console.WriteLine()

      ' Set the path of the config file.
      Dim configPath As String = "/aspnet"

      ' Get the Web application configuration object.
      Dim config As Configuration = _
      WebConfigurationManager.OpenWebConfiguration(configPath)

      ' Get the section related object.
      Dim configSection As AuthorizationSection = _
      CType(config.GetSection("system.web/authorization"), AuthorizationSection)

      ' Get the authorization rule collection.
      Dim authorizationRuleCollection As AuthorizationRuleCollection = _
      configSection.Rules()

      ' Create a CommaDelimitedStringCollection object.
      Dim myStrCollection As CommaDelimitedStringCollection = _
        New CommaDelimitedStringCollection()

      Dim i As Integer
      For i = 0 To authorizationRuleCollection.Count - 1 Step i + 1
        If authorizationRuleCollection.Get(i).Action.ToString().ToLower() _
          = "allow" Then
          ' Add values to the CommaDelimitedStringCollection object.
          myStrCollection.AddRange( _
            authorizationRuleCollection.Get(i).Users.ToString().Split( _
            ",".ToCharArray()))
        End If
      Next

      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()
    End Sub
  End Class
End Namespace

備註

這個類別代表串行化為字串元素逗號分隔清單的字串集合。

建構函式

CommaDelimitedStringCollection()

建立 CommaDelimitedStringCollection 類別的新執行個體。

屬性

Count

取得在 StringCollection 中所包含的字串數目。

(繼承來源 StringCollection)
IsModified

取得值,指定是否已經修改集合。

IsReadOnly

取得值,指出集合物件是否為唯讀。

IsSynchronized

取得值,這個值表示對 StringCollection 的存取是否同步 (安全執行緒)。

(繼承來源 StringCollection)
Item[Int32]

根據索引,取得或設定集合中的字串項目。

SyncRoot

取得可用以同步存取 StringCollection 的物件。

(繼承來源 StringCollection)

方法

Add(String)

將字串加入至逗號分隔的集合。

AddRange(String[])

將字串陣列中的所有字串加入至集合。

Clear()

清除集合。

Clone()

建立集合的複本。

Contains(String)

判斷指定的字串是否在 StringCollection 中。

(繼承來源 StringCollection)
CopyTo(String[], Int32)

複製整個 StringCollection 值至字串的一維陣列,由目標陣列的指定索引開始。

(繼承來源 StringCollection)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetEnumerator()

傳回逐一查看 StringEnumeratorStringCollection

(繼承來源 StringCollection)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IndexOf(String)

搜尋指定的字串,並傳回在 StringCollection 中第一個符合項目之以零起始的索引。

(繼承來源 StringCollection)
Insert(Int32, String)

將字串項目加入至集合中的指定索引處。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Remove(String)

從集合中移除字串項目。

RemoveAt(Int32)

移除 StringCollection 的指定索引處的字串。

(繼承來源 StringCollection)
SetReadOnly()

將集合物件設定為唯讀。

ToString()

傳回物件的字串表示。

明確介面實作

ICollection.CopyTo(Array, Int32)

從目標陣列的指定索引開始,將整個 StringCollection 複製到相容的一維 Array

(繼承來源 StringCollection)
IEnumerable.GetEnumerator()

傳回逐一查看 IEnumeratorStringCollection

(繼承來源 StringCollection)
IList.Add(Object)

將物件加入至 StringCollection 的末端。

(繼承來源 StringCollection)
IList.Contains(Object)

判斷某項目是否在 StringCollection 中。

(繼承來源 StringCollection)
IList.IndexOf(Object)

搜尋指定的 Object,並傳回在整個 StringCollection 中第一個符合項目之以零為起始的索引。

(繼承來源 StringCollection)
IList.Insert(Int32, Object)

將項目插入至 StringCollection 中指定的索引位置。

(繼承來源 StringCollection)
IList.IsFixedSize

取得值,指出 StringCollection 物件是否具有固定的大小。

(繼承來源 StringCollection)
IList.IsReadOnly

取得值,這個值表示 StringCollection 物件是否唯讀。

(繼承來源 StringCollection)
IList.Item[Int32]

在指定的索引位置上取得或設定項目。

(繼承來源 StringCollection)
IList.Remove(Object)

StringCollection 移除特定物件之第一個符合的元素。

(繼承來源 StringCollection)

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於

另請參閱