CommaDelimitedStringCollection 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示一組以逗號分隔的字串元素。 此類別無法獲得繼承。
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 型別。
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
備註
此類別代表一個字串集合,並以逗號分隔的字串元素列表序列化。
建構函式
| 名稱 | Description |
|---|---|
| CommaDelimitedStringCollection() |
建立一個新的類別實例 CommaDelimitedStringCollection 。 |
屬性
| 名稱 | Description |
|---|---|
| Count |
得到包含在 StringCollection中的字串數量。 (繼承來源 StringCollection) |
| IsModified |
會得到一個值,指定該集合是否被修改。 |
| IsReadOnly |
會取得一個值,表示該集合物件是否為唯讀。 |
| IsSynchronized |
取得值,指出是否同步存取 StringCollection (線程安全)。 (繼承來源 StringCollection) |
| Item[Int32] |
根據索引取得或設定集合中的字串元素。 |
| SyncRoot |
取得一個物件,可用來同步存取 StringCollection。 (繼承來源 StringCollection) |
方法
| 名稱 | Description |
|---|---|
| Add(String) |
為逗號分隔集合新增一串字串。 |
| AddRange(String[]) |
將字串陣列中的所有字串加入集合。 |
| Clear() |
清空了收款。 |
| Clone() |
建立該收藏的副本。 |
| Contains(String) |
判斷指定字串是否屬於 StringCollection。 (繼承來源 StringCollection) |
| CopyTo(String[], Int32) |
將整個 StringCollection 值複製到一維字串陣列,從目標陣列指定的索引開始。 (繼承來源 StringCollection) |
| Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
| GetEnumerator() |
回傳 a StringEnumerator ,使其遍歷 。StringCollection (繼承來源 StringCollection) |
| GetHashCode() |
做為預設哈希函式。 (繼承來源 Object) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| IndexOf(String) |
搜尋指定字串,並回傳 中首次出現 StringCollection的零為基礎索引。 (繼承來源 StringCollection) |
| Insert(Int32, String) |
在指定的索引處為集合加入一個字串元素。 |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| Remove(String) |
從集合中移除一個字串元素。 |
| RemoveAt(Int32) |
移除位於指定索引 StringCollection的字串。 (繼承來源 StringCollection) |
| SetReadOnly() |
將集合物件設為唯讀。 |
| ToString() |
回傳物件的字串表示。 |
明確介面實作
| 名稱 | Description |
|---|---|
| ICollection.CopyTo(Array, Int32) |
從目標陣列的指定索引開始,將整個 StringCollection 複製到相容的一維 Array。 (繼承來源 StringCollection) |
| IEnumerable.GetEnumerator() |
回傳 a IEnumerator ,使其遍歷 。StringCollection (繼承來源 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) |
擴充方法
| 名稱 | Description |
|---|---|
| AsParallel(IEnumerable) |
啟用查詢的平行處理。 |
| AsQueryable(IEnumerable) |
將 IEnumerable 轉換成 IQueryable。 |
| Cast<TResult>(IEnumerable) |
將 IEnumerable 的項目轉換成指定的型別。 |
| OfType<TResult>(IEnumerable) |
根據指定的型別篩選 IEnumerable 的專案。 |