StringDictionary 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
使用強類型索引鍵和值,將雜湊資料表實作為字串,而非物件。
public ref class StringDictionary : System::Collections::IEnumerable
public class StringDictionary : System.Collections.IEnumerable
[System.Serializable]
public class StringDictionary : System.Collections.IEnumerable
type StringDictionary = class
interface IEnumerable
[<System.Serializable>]
type StringDictionary = class
interface IEnumerable
Public Class StringDictionary
Implements IEnumerable
- 繼承
-
StringDictionary
- 屬性
- 實作
範例
下列程式代碼範例示範 的數個屬性和方法 StringDictionary。
#using <System.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
void PrintKeysAndValues2( StringDictionary^ myCol );
void PrintKeysAndValues3( StringDictionary^ myCol );
int main()
{
// Creates and initializes a new StringDictionary.
StringDictionary^ myCol = gcnew StringDictionary;
myCol->Add( "red", "rojo" );
myCol->Add( "green", "verde" );
myCol->Add( "blue", "azul" );
// Display the contents of the collection using the enumerator.
Console::WriteLine( "Displays the elements using the IEnumerator:" );
PrintKeysAndValues2( myCol );
// Display the contents of the collection using the Keys, Values, Count, and Item properties.
Console::WriteLine( "Displays the elements using the Keys, Values, Count, and Item properties:" );
PrintKeysAndValues3( myCol );
// Copies the StringDictionary to an array with DictionaryEntry elements.
array<DictionaryEntry>^myArr = gcnew array<DictionaryEntry>(myCol->Count);
myCol->CopyTo( myArr, 0 );
// Displays the values in the array.
Console::WriteLine( "Displays the elements in the array:" );
Console::WriteLine( " KEY VALUE" );
for ( int i = 0; i < myArr->Length; i++ )
Console::WriteLine( " {0,-10} {1}", myArr[ i ].Key, myArr[ i ].Value );
Console::WriteLine();
// Searches for a value.
if ( myCol->ContainsValue( "amarillo" ) )
Console::WriteLine( "The collection contains the value \"amarillo\"." );
else
Console::WriteLine( "The collection does not contain the value \"amarillo\"." );
Console::WriteLine();
// Searches for a key and deletes it.
if ( myCol->ContainsKey( "green" ) )
myCol->Remove( "green" );
Console::WriteLine( "The collection contains the following elements after removing \"green\":" );
PrintKeysAndValues2( myCol );
// Clears the entire collection.
myCol->Clear();
Console::WriteLine( "The collection contains the following elements after it is cleared:" );
PrintKeysAndValues2( myCol );
}
// Uses the enumerator.
void PrintKeysAndValues2( StringDictionary^ myCol )
{
IEnumerator^ myEnumerator = myCol->GetEnumerator();
DictionaryEntry de;
Console::WriteLine( " KEY VALUE" );
while ( myEnumerator->MoveNext() )
{
de = *dynamic_cast<DictionaryEntry^>(myEnumerator->Current);
Console::WriteLine( " {0,-25} {1}", de.Key, de.Value );
}
Console::WriteLine();
}
// Uses the Keys, Values, Count, and Item properties.
void PrintKeysAndValues3( StringDictionary^ myCol )
{
array<String^>^myKeys = gcnew array<String^>(myCol->Count);
myCol->Keys->CopyTo( myKeys, 0 );
Console::WriteLine( " INDEX KEY VALUE" );
for ( int i = 0; i < myCol->Count; i++ )
Console::WriteLine( " {0,-5} {1,-25} {2}", i, myKeys[ i ], myCol[ myKeys[ i ] ] );
Console::WriteLine();
}
/*
This code produces the following output.
Displays the elements using the IEnumerator:
KEY VALUE
red rojo
blue azul
green verde
Displays the elements using the Keys, Values, Count, and Item properties:
INDEX KEY VALUE
0 red rojo
1 blue azul
2 green verde
Displays the elements in the array:
KEY VALUE
red rojo
blue azul
green verde
The collection does not contain the value "amarillo".
The collection contains the following elements after removing "green":
KEY VALUE
red rojo
blue azul
The collection contains the following elements after it is cleared:
KEY VALUE
*/
using System;
using System.Collections;
using System.Collections.Specialized;
public class SamplesStringDictionary {
public static void Main() {
// Creates and initializes a new StringDictionary.
StringDictionary myCol = new StringDictionary();
myCol.Add( "red", "rojo" );
myCol.Add( "green", "verde" );
myCol.Add( "blue", "azul" );
// Display the contents of the collection using foreach. This is the preferred method.
Console.WriteLine( "Displays the elements using foreach:" );
PrintKeysAndValues1( myCol );
// Display the contents of the collection using the enumerator.
Console.WriteLine( "Displays the elements using the IEnumerator:" );
PrintKeysAndValues2( myCol );
// Display the contents of the collection using the Keys, Values, Count, and Item properties.
Console.WriteLine( "Displays the elements using the Keys, Values, Count, and Item properties:" );
PrintKeysAndValues3( myCol );
// Copies the StringDictionary to an array with DictionaryEntry elements.
DictionaryEntry[] myArr = new DictionaryEntry[myCol.Count];
myCol.CopyTo( myArr, 0 );
// Displays the values in the array.
Console.WriteLine( "Displays the elements in the array:" );
Console.WriteLine( " KEY VALUE" );
for ( int i = 0; i < myArr.Length; i++ )
Console.WriteLine( " {0,-10} {1}", myArr[i].Key, myArr[i].Value );
Console.WriteLine();
// Searches for a value.
if ( myCol.ContainsValue( "amarillo" ) )
Console.WriteLine( "The collection contains the value \"amarillo\"." );
else
Console.WriteLine( "The collection does not contain the value \"amarillo\"." );
Console.WriteLine();
// Searches for a key and deletes it.
if ( myCol.ContainsKey( "green" ) )
myCol.Remove( "green" );
Console.WriteLine( "The collection contains the following elements after removing \"green\":" );
PrintKeysAndValues1( myCol );
// Clears the entire collection.
myCol.Clear();
Console.WriteLine( "The collection contains the following elements after it is cleared:" );
PrintKeysAndValues1( myCol );
}
// Uses the foreach statement which hides the complexity of the enumerator.
// NOTE: The foreach statement is the preferred way of enumerating the contents of a collection.
public static void PrintKeysAndValues1( StringDictionary myCol ) {
Console.WriteLine( " KEY VALUE" );
foreach ( DictionaryEntry de in myCol )
Console.WriteLine( " {0,-25} {1}", de.Key, de.Value );
Console.WriteLine();
}
// Uses the enumerator.
// NOTE: The foreach statement is the preferred way of enumerating the contents of a collection.
public static void PrintKeysAndValues2( StringDictionary myCol ) {
IEnumerator myEnumerator = myCol.GetEnumerator();
DictionaryEntry de;
Console.WriteLine( " KEY VALUE" );
while ( myEnumerator.MoveNext() ) {
de = (DictionaryEntry) myEnumerator.Current;
Console.WriteLine( " {0,-25} {1}", de.Key, de.Value );
}
Console.WriteLine();
}
// Uses the Keys, Values, Count, and Item properties.
public static void PrintKeysAndValues3( StringDictionary myCol ) {
String[] myKeys = new String[myCol.Count];
myCol.Keys.CopyTo( myKeys, 0 );
Console.WriteLine( " INDEX KEY VALUE" );
for ( int i = 0; i < myCol.Count; i++ )
Console.WriteLine( " {0,-5} {1,-25} {2}", i, myKeys[i], myCol[myKeys[i]] );
Console.WriteLine();
}
}
/*
This code produces the following output.
Displays the elements using foreach:
KEY VALUE
red rojo
blue azul
green verde
Displays the elements using the IEnumerator:
KEY VALUE
red rojo
blue azul
green verde
Displays the elements using the Keys, Values, Count, and Item properties:
INDEX KEY VALUE
0 red rojo
1 blue azul
2 green verde
Displays the elements in the array:
KEY VALUE
red rojo
blue azul
green verde
The collection does not contain the value "amarillo".
The collection contains the following elements after removing "green":
KEY VALUE
red rojo
blue azul
The collection contains the following elements after it is cleared:
KEY VALUE
*/
Imports System.Collections
Imports System.Collections.Specialized
Public Class SamplesStringDictionary
Public Shared Sub Main()
' Creates and initializes a new StringDictionary.
Dim myCol As New StringDictionary()
myCol.Add("red", "rojo")
myCol.Add("green", "verde")
myCol.Add("blue", "azul")
' Display the contents of the collection using For Each. This is the preferred method.
Console.WriteLine("Displays the elements using For Each:")
PrintKeysAndValues1(myCol)
' Display the contents of the collection using the enumerator.
Console.WriteLine("Displays the elements using the IEnumerator:")
PrintKeysAndValues2(myCol)
' Display the contents of the collection using the Keys, Values, Count, and Item properties.
Console.WriteLine("Displays the elements using the Keys, Values, Count, and Item properties:")
PrintKeysAndValues3(myCol)
' Copies the StringDictionary to an array with DictionaryEntry elements.
Dim myArr(myCol.Count) As DictionaryEntry
myCol.CopyTo(myArr, 0)
' Displays the values in the array.
Console.WriteLine("Displays the elements in the array:")
Console.WriteLine(" KEY VALUE")
Dim i As Integer
For i = 0 To myArr.Length - 1
Console.WriteLine(" {0,-10} {1}", myArr(i).Key, myArr(i).Value)
Next i
Console.WriteLine()
' Searches for a value.
If myCol.ContainsValue("amarillo") Then
Console.WriteLine("The collection contains the value ""amarillo"".")
Else
Console.WriteLine("The collection does not contain the value ""amarillo"".")
End If
Console.WriteLine()
' Searches for a key and deletes it.
If myCol.ContainsKey("green") Then
myCol.Remove("green")
End If
Console.WriteLine("The collection contains the following elements after removing ""green"":")
PrintKeysAndValues1(myCol)
' Clears the entire collection.
myCol.Clear()
Console.WriteLine("The collection contains the following elements after it is cleared:")
PrintKeysAndValues1(myCol)
End Sub
' Uses the For Each statement which hides the complexity of the enumerator.
' NOTE: The For Each statement is the preferred way of enumerating the contents of a collection.
Public Shared Sub PrintKeysAndValues1(myCol As StringDictionary)
Console.WriteLine(" KEY VALUE")
Dim de As DictionaryEntry
For Each de In myCol
Console.WriteLine(" {0,-25} {1}", de.Key, de.Value)
Next de
Console.WriteLine()
End Sub
' Uses the enumerator.
' NOTE: The For Each statement is the preferred way of enumerating the contents of a collection.
Public Shared Sub PrintKeysAndValues2(myCol As StringDictionary)
Dim myEnumerator As IEnumerator = myCol.GetEnumerator()
Dim de As DictionaryEntry
Console.WriteLine(" KEY VALUE")
While myEnumerator.MoveNext()
de = CType(myEnumerator.Current, DictionaryEntry)
Console.WriteLine(" {0,-25} {1}", de.Key, de.Value)
End While
Console.WriteLine()
End Sub
' Uses the Keys, Values, Count, and Item properties.
Public Shared Sub PrintKeysAndValues3(myCol As StringDictionary)
Dim myKeys(myCol.Count) As String
myCol.Keys.CopyTo(myKeys, 0)
Console.WriteLine(" INDEX KEY VALUE")
Dim i As Integer
For i = 0 To myCol.Count - 1
Console.WriteLine(" {0,-5} {1,-25} {2}", i, myKeys(i), myCol(myKeys(i)))
Next i
Console.WriteLine()
End Sub
End Class
'This code produces the following output.
'
'Displays the elements using For Each:
' KEY VALUE
' red rojo
' blue azul
' green verde
'
'Displays the elements using the IEnumerator:
' KEY VALUE
' red rojo
' blue azul
' green verde
'
'Displays the elements using the Keys, Values, Count, and Item properties:
' INDEX KEY VALUE
' 0 red rojo
' 1 blue azul
' 2 green verde
'
'Displays the elements in the array:
' KEY VALUE
' red rojo
' blue azul
' green verde
'
'
'The collection does not contain the value "amarillo".
'
'The collection contains the following elements after removing "green":
' KEY VALUE
' red rojo
' blue azul
'
'The collection contains the following elements after it is cleared:
' KEY VALUE
備註
索引鍵不能是 null
,但值可以。
索引鍵會以不區分大小寫的方式處理;它會在與字串字典搭配使用之前,先轉譯成小寫。
在 .NET Framework 1.0 版中,此類別會使用區分文化特性的字串比較。 不過,在 .NET Framework 1.1 版和更新版本中,這個類別會在比較字串時使用CultureInfo.InvariantCulture。 如需文化特性如何影響比較和排序的詳細資訊,請參閱 執行 Culture-Insensitive 字串作業。
建構函式
StringDictionary() |
初始化 StringDictionary 類別的新執行個體。 |
屬性
Count |
取得 StringDictionary 中的索引鍵/值組配對的數目。 |
IsSynchronized |
取得值,這個值表示對 StringDictionary 的存取是否同步 (安全執行緒)。 |
Item[String] |
取得或設定與指定之索引鍵相關聯的值。 |
Keys |
取得 StringDictionary 中的索引鍵集合。 |
SyncRoot |
取得可用以同步存取 StringDictionary 的物件。 |
Values |
取得 StringDictionary 中的值集合。 |
方法
Add(String, String) |
將有指定索引鍵和數值項目加入 StringDictionary 中。 |
Clear() |
移除 StringDictionary 的所有項目。 |
ContainsKey(String) |
判斷 StringDictionary 是否含有特定的索引鍵。 |
ContainsValue(String) |
判斷 StringDictionary 是否含有特定值。 |
CopyTo(Array, Int32) |
將字串字典值複製至在指定索引處的一維 Array 執行個體。 |
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
GetEnumerator() |
傳回可對字串字典內容逐一查看的列舉程式。 |
GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
Remove(String) |
將具有指定索引鍵的項目從字串字典移除。 |
ToString() |
傳回代表目前物件的字串。 (繼承來源 Object) |
擴充方法
Cast<TResult>(IEnumerable) |
將 IEnumerable 的項目轉換成指定的型別。 |
OfType<TResult>(IEnumerable) |
根據指定的型別來篩選 IEnumerable 的項目。 |
AsParallel(IEnumerable) |
啟用查詢的平行化作業。 |
AsQueryable(IEnumerable) |
將 IEnumerable 轉換成 IQueryable。 |
適用於
執行緒安全性
Visual Basic 中的公用靜態 (Shared
) 此類型的成員是安全線程。 並非所有的執行個體成員都是安全執行緒。
此實作不提供的同步處理 (線程安全) 包裝StringDictionary函式,但衍生類別可以使用 屬性建立自己的同步版本StringDictionarySyncRoot。
透過集合進行列舉在本質上並非安全執行緒程序。 即使集合經過同步化,其他的執行緒仍可修改該集合,使列舉值擲回例外狀況。 若要保證列舉過程的執行緒安全,您可以在整個列舉過程中鎖定集合,或攔截由其他執行緒的變更所造成的例外狀況。