OrderedDictionary 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示可依索引鍵或索引存取的索引鍵/值組集合。
public ref class OrderedDictionary : System::Collections::IDictionary, System::Collections::Specialized::IOrderedDictionary
public ref class OrderedDictionary : System::Collections::IDictionary, System::Collections::Specialized::IOrderedDictionary, System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializable
public ref class OrderedDictionary : System::Collections::Specialized::IOrderedDictionary, System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializable
public class OrderedDictionary : System.Collections.IDictionary, System.Collections.Specialized.IOrderedDictionary
public class OrderedDictionary : System.Collections.IDictionary, System.Collections.Specialized.IOrderedDictionary, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
[System.Serializable]
public class OrderedDictionary : System.Collections.IDictionary, System.Collections.Specialized.IOrderedDictionary, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
public class OrderedDictionary : System.Collections.Specialized.IOrderedDictionary, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
type OrderedDictionary = class
interface ICollection
interface IEnumerable
interface IDictionary
interface IOrderedDictionary
type OrderedDictionary = class
interface ICollection
interface IEnumerable
interface IDictionary
interface IOrderedDictionary
interface IDeserializationCallback
interface ISerializable
type OrderedDictionary = class
interface ICollection
interface IEnumerable
interface IDictionary
interface IOrderedDictionary
interface ISerializable
interface IDeserializationCallback
[<System.Serializable>]
type OrderedDictionary = class
interface IOrderedDictionary
interface IDictionary
interface ICollection
interface IEnumerable
interface ISerializable
interface IDeserializationCallback
Public Class OrderedDictionary
Implements IDictionary, IOrderedDictionary
Public Class OrderedDictionary
Implements IDeserializationCallback, IDictionary, IOrderedDictionary, ISerializable
Public Class OrderedDictionary
Implements IDeserializationCallback, IOrderedDictionary, ISerializable
- 繼承
-
OrderedDictionary
- 衍生
- 屬性
- 實作
範例
下列程式代碼範例示範集合的OrderedDictionary建立、母體擴展和修改,以及兩種顯示 內容的OrderedDictionary技巧:一種是使用 和 Values 屬性,另一種是Keys透過 GetEnumerator 方法建立列舉值。
// The following code example enumerates the elements of a OrderedDictionary.
#using <System.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
public ref class OrderedDictionarySample
{
public:
static void Main()
{
// Creates and initializes a OrderedDictionary.
OrderedDictionary^ myOrderedDictionary = gcnew OrderedDictionary();
myOrderedDictionary->Add("testKey1", "testValue1");
myOrderedDictionary->Add("testKey2", "testValue2");
myOrderedDictionary->Add("keyToDelete", "valueToDelete");
myOrderedDictionary->Add("testKey3", "testValue3");
ICollection^ keyCollection = myOrderedDictionary->Keys;
ICollection^ valueCollection = myOrderedDictionary->Values;
// Display the contents using the key and value collections
DisplayContents(keyCollection, valueCollection, myOrderedDictionary->Count);
// Modifying the OrderedDictionary
if (!myOrderedDictionary->IsReadOnly)
{
// Insert a new key to the beginning of the OrderedDictionary
myOrderedDictionary->Insert(0, "insertedKey1", "insertedValue1");
// Modify the value of the entry with the key "testKey2"
myOrderedDictionary["testKey2"] = "modifiedValue";
// Remove the last entry from the OrderedDictionary: "testKey3"
myOrderedDictionary->RemoveAt(myOrderedDictionary->Count - 1);
// Remove the "keyToDelete" entry, if it exists
if (myOrderedDictionary->Contains("keyToDelete"))
{
myOrderedDictionary->Remove("keyToDelete");
}
}
Console::WriteLine(
"{0}Displaying the entries of a modified OrderedDictionary.",
Environment::NewLine);
DisplayContents(keyCollection, valueCollection, myOrderedDictionary->Count);
// Clear the OrderedDictionary and add new values
myOrderedDictionary->Clear();
myOrderedDictionary->Add("newKey1", "newValue1");
myOrderedDictionary->Add("newKey2", "newValue2");
myOrderedDictionary->Add("newKey3", "newValue3");
// Display the contents of the "new" Dictionary using an enumerator
IDictionaryEnumerator^ myEnumerator =
myOrderedDictionary->GetEnumerator();
Console::WriteLine(
"{0}Displaying the entries of a \"new\" OrderedDictionary.",
Environment::NewLine);
DisplayEnumerator(myEnumerator);
}
// Displays the contents of the OrderedDictionary from its keys and values
static void DisplayContents(
ICollection^ keyCollection, ICollection^ valueCollection, int dictionarySize)
{
array<String^>^ myKeys = gcnew array<String^>(dictionarySize);
array<String^>^ myValues = gcnew array<String^>(dictionarySize);
keyCollection->CopyTo(myKeys, 0);
valueCollection->CopyTo(myValues, 0);
// Displays the contents of the OrderedDictionary
Console::WriteLine(" INDEX KEY VALUE");
for (int i = 0; i < dictionarySize; i++)
{
Console::WriteLine(" {0,-5} {1,-25} {2}",
i, myKeys[i], myValues[i]);
}
Console::WriteLine();
}
// Displays the contents of the OrderedDictionary using its enumerator
static void DisplayEnumerator(IDictionaryEnumerator^ myEnumerator)
{
Console::WriteLine(" KEY VALUE");
while (myEnumerator->MoveNext())
{
Console::WriteLine(" {0,-25} {1}",
myEnumerator->Key, myEnumerator->Value);
}
}
};
int main()
{
OrderedDictionarySample::Main();
}
/*
This code produces the following output.
INDEX KEY VALUE
0 testKey1 testValue1
1 testKey2 testValue2
2 keyToDelete valueToDelete
3 testKey3 testValue3
Displaying the entries of a modified OrderedDictionary.
INDEX KEY VALUE
0 insertedKey1 insertedValue1
1 testKey1 testValue1
2 testKey2 modifiedValue
Displaying the entries of a "new" OrderedDictionary.
KEY VALUE
newKey1 newValue1
newKey2 newValue2
newKey3 newValue3
*/
// The following code example enumerates the elements of a OrderedDictionary.
using System;
using System.Collections;
using System.Collections.Specialized;
public class OrderedDictionarySample
{
public static void Main()
{
// Creates and initializes a OrderedDictionary.
OrderedDictionary myOrderedDictionary = new OrderedDictionary();
myOrderedDictionary.Add("testKey1", "testValue1");
myOrderedDictionary.Add("testKey2", "testValue2");
myOrderedDictionary.Add("keyToDelete", "valueToDelete");
myOrderedDictionary.Add("testKey3", "testValue3");
ICollection keyCollection = myOrderedDictionary.Keys;
ICollection valueCollection = myOrderedDictionary.Values;
// Display the contents using the key and value collections
DisplayContents(keyCollection, valueCollection, myOrderedDictionary.Count);
// Modifying the OrderedDictionary
if (!myOrderedDictionary.IsReadOnly)
{
// Insert a new key to the beginning of the OrderedDictionary
myOrderedDictionary.Insert(0, "insertedKey1", "insertedValue1");
// Modify the value of the entry with the key "testKey2"
myOrderedDictionary["testKey2"] = "modifiedValue";
// Remove the last entry from the OrderedDictionary: "testKey3"
myOrderedDictionary.RemoveAt(myOrderedDictionary.Count - 1);
// Remove the "keyToDelete" entry, if it exists
if (myOrderedDictionary.Contains("keyToDelete"))
{
myOrderedDictionary.Remove("keyToDelete");
}
}
Console.WriteLine(
"{0}Displaying the entries of a modified OrderedDictionary.",
Environment.NewLine);
DisplayContents(keyCollection, valueCollection, myOrderedDictionary.Count);
// Clear the OrderedDictionary and add new values
myOrderedDictionary.Clear();
myOrderedDictionary.Add("newKey1", "newValue1");
myOrderedDictionary.Add("newKey2", "newValue2");
myOrderedDictionary.Add("newKey3", "newValue3");
// Display the contents of the "new" Dictionary using an enumerator
IDictionaryEnumerator myEnumerator =
myOrderedDictionary.GetEnumerator();
Console.WriteLine(
"{0}Displaying the entries of a \"new\" OrderedDictionary.",
Environment.NewLine);
DisplayEnumerator(myEnumerator);
}
// Displays the contents of the OrderedDictionary from its keys and values
public static void DisplayContents(
ICollection keyCollection, ICollection valueCollection, int dictionarySize)
{
String[] myKeys = new String[dictionarySize];
String[] myValues = new String[dictionarySize];
keyCollection.CopyTo(myKeys, 0);
valueCollection.CopyTo(myValues, 0);
// Displays the contents of the OrderedDictionary
Console.WriteLine(" INDEX KEY VALUE");
for (int i = 0; i < dictionarySize; i++)
{
Console.WriteLine(" {0,-5} {1,-25} {2}",
i, myKeys[i], myValues[i]);
}
Console.WriteLine();
}
// Displays the contents of the OrderedDictionary using its enumerator
public static void DisplayEnumerator(IDictionaryEnumerator myEnumerator)
{
Console.WriteLine(" KEY VALUE");
while (myEnumerator.MoveNext())
{
Console.WriteLine(" {0,-25} {1}",
myEnumerator.Key, myEnumerator.Value);
}
}
}
/*
This code produces the following output.
INDEX KEY VALUE
0 testKey1 testValue1
1 testKey2 testValue2
2 keyToDelete valueToDelete
3 testKey3 testValue3
Displaying the entries of a modified OrderedDictionary.
INDEX KEY VALUE
0 insertedKey1 insertedValue1
1 testKey1 testValue1
2 testKey2 modifiedValue
Displaying the entries of a "new" OrderedDictionary.
KEY VALUE
newKey1 newValue1
newKey2 newValue2
newKey3 newValue3
*/
' The following code example enumerates the elements of a OrderedDictionary.
Imports System.Collections
Imports System.Collections.Specialized
Public Class OrderedDictionarySample
Public Shared Sub Main()
' Creates and initializes a OrderedDictionary.
Dim myOrderedDictionary As New OrderedDictionary()
myOrderedDictionary.Add("testKey1", "testValue1")
myOrderedDictionary.Add("testKey2", "testValue2")
myOrderedDictionary.Add("keyToDelete", "valueToDelete")
myOrderedDictionary.Add("testKey3", "testValue3")
Dim keyCollection As ICollection = myOrderedDictionary.Keys
Dim valueCollection As ICollection = myOrderedDictionary.Values
' Display the contents Imports the key and value collections
DisplayContents( _
keyCollection, valueCollection, myOrderedDictionary.Count)
' Modifying the OrderedDictionary
If Not myOrderedDictionary.IsReadOnly Then
' Insert a new key to the beginning of the OrderedDictionary
myOrderedDictionary.Insert(0, "insertedKey1", "insertedValue1")
' Modify the value of the entry with the key "testKey2"
myOrderedDictionary("testKey2") = "modifiedValue"
' Remove the last entry from the OrderedDictionary: "testKey3"
myOrderedDictionary.RemoveAt(myOrderedDictionary.Count - 1)
' Remove the "keyToDelete" entry, if it exists
If (myOrderedDictionary.Contains("keyToDelete")) Then
myOrderedDictionary.Remove("keyToDelete")
End If
End If
Console.WriteLine( _
"{0}Displaying the entries of a modified OrderedDictionary.", _
Environment.NewLine)
DisplayContents( _
keyCollection, valueCollection, myOrderedDictionary.Count)
' Clear the OrderedDictionary and add new values
myOrderedDictionary.Clear()
myOrderedDictionary.Add("newKey1", "newValue1")
myOrderedDictionary.Add("newKey2", "newValue2")
myOrderedDictionary.Add("newKey3", "newValue3")
' Display the contents of the "new" Dictionary Imports an enumerator
Dim myEnumerator As IDictionaryEnumerator = _
myOrderedDictionary.GetEnumerator()
Console.WriteLine( _
"{0}Displaying the entries of a 'new' OrderedDictionary.", _
Environment.NewLine)
DisplayEnumerator(myEnumerator)
End Sub
' Displays the contents of the OrderedDictionary from its keys and values
Public Shared Sub DisplayContents( _
ByVal keyCollection As ICollection, _
ByVal valueCollection As ICollection, ByVal dictionarySize As Integer)
Dim myKeys(dictionarySize) As [String]
Dim myValues(dictionarySize) As [String]
keyCollection.CopyTo(myKeys, 0)
valueCollection.CopyTo(myValues, 0)
' Displays the contents of the OrderedDictionary
Console.WriteLine(" INDEX KEY VALUE")
Dim i As Integer
For i = 0 To dictionarySize - 1
Console.WriteLine(" {0,-5} {1,-25} {2}", _
i, myKeys(i), myValues(i))
Next i
Console.WriteLine()
End Sub
' Displays the contents of the OrderedDictionary using its enumerator
Public Shared Sub DisplayEnumerator( _
ByVal myEnumerator As IDictionaryEnumerator)
Console.WriteLine(" KEY VALUE")
While myEnumerator.MoveNext()
Console.WriteLine(" {0,-25} {1}", _
myEnumerator.Key, myEnumerator.Value)
End While
End Sub
End Class
'This code produces the following output.
'
' INDEX KEY VALUE
'0: testKey1(testValue1)
'1: testKey2(testValue2)
'2: keyToDelete(valueToDelete)
'3: testKey3(testValue3)
'
'
'Displaying the entries of a modified OrderedDictionary.
' INDEX KEY VALUE
'0: insertedKey1(insertedValue1)
'1: testKey1(testValue1)
'2: testKey2(modifiedValue)
'
'
'Displaying the entries of a "new" OrderedDictionary.
' KEY(VALUE)
' newKey1(newValue1)
' newKey2(newValue2)
' newKey3(newValue3)
備註
每個元素都是儲存在物件中的 DictionaryEntry 索引鍵/值組。 索引鍵不能是 null
,但值可以是 。
的 OrderedDictionary 元素不會依索引鍵排序,與類別的 SortedDictionary<TKey,TValue> 元素不同。 您可以透過索引鍵或索引來存取元素。
foreach
Visual Basic 中 C# 語言 (For Each
的語句) 會傳回集合中每個元素類型的 物件。 因為集合的每個元素 OrderedDictionary 都是索引鍵/值組,所以元素類型不是索引鍵的類型或值的型別。 相反地,元素類型為 DictionaryEntry。 下列程式代碼顯示 C#、Visual Basic 和 C++ 語法。
for each (DictionaryEntry de in myOrderedDictionary)
{
//...
}
foreach (DictionaryEntry de in myOrderedDictionary)
{
//...
}
For Each de As DictionaryEntry In myOrderedDictionary
'...
Next de
語句 foreach
是列舉值周圍的包裝函式,它只允許讀取集合,而不寫入集合。
建構函式
OrderedDictionary() |
初始化 OrderedDictionary 類別的新執行個體。 |
OrderedDictionary(IEqualityComparer) |
使用指定之比較子來初始化 OrderedDictionary 類別的新執行個體。 |
OrderedDictionary(Int32) |
使用指定之初始容量來初始化 OrderedDictionary 類別的新執行個體。 |
OrderedDictionary(Int32, IEqualityComparer) |
使用指定的初始容量和比較子,初始化 OrderedDictionary 類別的新執行個體。 |
OrderedDictionary(SerializationInfo, StreamingContext) |
已淘汰.
使用指定的 OrderedDictionary 和 SerializationInfo 物件,初始化 StreamingContext 類別的執行個體,這個執行個體是可序列化的。 |
屬性
Count |
取得包含在 OrderedDictionary 集合中的索引鍵/值組數目。 |
IsReadOnly |
取得值,表示 OrderedDictionary 集合是否為唯讀。 |
Item[Int32] |
取得或設定指定之索引處的值。 |
Item[Object] |
取得或設定具有指定之索引鍵的值。 |
Keys |
取得 ICollection 物件,其中包含 OrderedDictionary 集合中的索引鍵。 |
Values |
取得 ICollection 物件,其中包含 OrderedDictionary 集合中的值。 |
方法
Add(Object, Object) |
將具有指定之索引鍵和值的元素加入至含有最低可用索引的 OrderedDictionary 集合中。 |
AsReadOnly() |
傳回目前 OrderedDictionary 集合的唯讀複本。 |
Clear() |
從 OrderedDictionary 集合移除所有元素。 |
Contains(Object) |
判斷 OrderedDictionary 集合是否包含特定索引鍵。 |
CopyTo(Array, Int32) |
將 OrderedDictionary 元素複製到指定之索引處的一維 Array 物件。 |
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
GetEnumerator() |
傳回會逐一查看 IDictionaryEnumerator 集合的 OrderedDictionary 物件。 |
GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
GetObjectData(SerializationInfo, StreamingContext) |
已淘汰.
實作 ISerializable 介面,並傳回序列化 OrderedDictionary 集合所需的資料。 |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
Insert(Int32, Object, Object) |
使用指定的索引鍵和值,將新元素插入 OrderedDictionary 集合中指定的索引處。 |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
OnDeserialization(Object) |
實作 ISerializable 介面並在還原序列化完成時,由還原序列化事件回呼。 |
Remove(Object) |
從 OrderedDictionary 集合移除具有指定之索引鍵的元素。 |
RemoveAt(Int32) |
從 OrderedDictionary 集合移除指定之索引處的元素。 |
ToString() |
傳回代表目前物件的字串。 (繼承來源 Object) |
明確介面實作
ICollection.IsSynchronized |
取得值,指出對 OrderedDictionary 物件的存取是否已同步處理 (安全執行緒)。 |
ICollection.SyncRoot |
取得可用來同步處理對 OrderedDictionary 物件之存取的物件。 |
IDeserializationCallback.OnDeserialization(Object) |
實作 ISerializable 介面並在還原序列化完成時,由還原序列化事件回呼。 |
IDictionary.IsFixedSize |
取得值,指出 OrderedDictionary 是否有固定的大小。 |
IEnumerable.GetEnumerator() |
傳回會逐一查看 IDictionaryEnumerator 集合的 OrderedDictionary 物件。 |
擴充方法
Cast<TResult>(IEnumerable) |
將 IEnumerable 的項目轉換成指定的型別。 |
OfType<TResult>(IEnumerable) |
根據指定的型別來篩選 IEnumerable 的項目。 |
AsParallel(IEnumerable) |
啟用查詢的平行化作業。 |
AsQueryable(IEnumerable) |
將 IEnumerable 轉換成 IQueryable。 |