OrderedDictionary Classe
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Représente une collection de paires clé/valeur accessibles par la clé ou l’index.
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
[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
[<System.Serializable>]
type OrderedDictionary = class
interface IOrderedDictionary
interface IDictionary
interface ICollection
interface IEnumerable
interface ISerializable
interface IDeserializationCallback
type OrderedDictionary = class
interface ICollection
interface IEnumerable
interface IDictionary
interface IOrderedDictionary
interface IDeserializationCallback
interface ISerializable
Public Class OrderedDictionary
Implements IDictionary, IOrderedDictionary
Public Class OrderedDictionary
Implements IDeserializationCallback, IDictionary, IOrderedDictionary, ISerializable
Public Class OrderedDictionary
Implements IDeserializationCallback, IOrderedDictionary, ISerializable
- Héritage
-
OrderedDictionary
- Dérivé
- Attributs
- Implémente
Exemples
L’exemple de code suivant illustre la création, la population et la modification d’une OrderedDictionary collection, ainsi que deux techniques d’affichage du contenu de l’objet OrderedDictionary: l’une utilisant les Keys propriétés et Values l’autre créant un énumérateur par le biais de la GetEnumerator méthode.
// 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)
Remarques
Chaque élément est une paire clé/valeur stockée dans un DictionaryEntry objet. Une clé ne peut pas être null, mais une valeur peut être.
Les éléments d’un élément OrderedDictionary ne sont pas triés par la clé, contrairement aux éléments d’une SortedDictionary<TKey,TValue> classe. Vous pouvez accéder aux éléments par la clé ou par l’index.
L’instruction foreach du langage C# (For Each dans Visual Basic) retourne des objets qui sont du type de chaque élément de la collection. Étant donné que chaque élément de la OrderedDictionary collection est une paire clé/valeur, le type d’élément n’est pas le type de la clé ou le type de la valeur. Au lieu de cela, le type d’élément est DictionaryEntry. Le code suivant montre la syntaxe.
foreach (DictionaryEntry de in myOrderedDictionary)
{
//...
}
For Each de As DictionaryEntry In myOrderedDictionary
'...
Next de
L’instruction foreach est un wrapper autour de l’énumérateur, qui autorise uniquement la lecture, pas l’écriture dans la collection.
Constructeurs
| Nom | Description |
|---|---|
| OrderedDictionary() |
Initialise une nouvelle instance de la classe OrderedDictionary. |
| OrderedDictionary(IEqualityComparer) |
Initialise une nouvelle instance de la classe à l’aide OrderedDictionary du comparateur spécifié. |
| OrderedDictionary(Int32, IEqualityComparer) |
Initialise une nouvelle instance de la OrderedDictionary classe à l’aide de la capacité initiale et du comparateur spécifiés. |
| OrderedDictionary(Int32) |
Initialise une nouvelle instance de la OrderedDictionary classe à l’aide de la capacité initiale spécifiée. |
| OrderedDictionary(SerializationInfo, StreamingContext) |
Initialise une nouvelle instance de la OrderedDictionary classe sérialisable à l’aide des objets et StreamingContext des objets spécifiésSerializationInfo. |
Propriétés
| Nom | Description |
|---|---|
| Count |
Obtient le nombre de paires clé/valeurs contenues dans la OrderedDictionary collection. |
| IsReadOnly |
Obtient une valeur indiquant si la OrderedDictionary collection est en lecture seule. |
| Item[Int32] |
Obtient ou définit la valeur à l’index spécifié. |
| Item[Object] |
Obtient ou définit la valeur avec la clé spécifiée. |
| Keys |
Obtient un ICollection objet contenant les clés de la OrderedDictionary collection. |
| Values |
Obtient un ICollection objet contenant les valeurs de la OrderedDictionary collection. |
Méthodes
| Nom | Description |
|---|---|
| Add(Object, Object) |
Ajoute une entrée avec la clé et la valeur spécifiées dans la OrderedDictionary collection avec l’index disponible le plus bas. |
| AsReadOnly() |
Renvoie une copie en lecture seule de la collection active OrderedDictionary . |
| Clear() |
Supprime tous les éléments de la OrderedDictionary collection. |
| Contains(Object) |
Détermine si la OrderedDictionary collection contient une clé spécifique. |
| CopyTo(Array, Int32) |
Copie les OrderedDictionary éléments dans un objet unidimensionnel Array à l’index spécifié. |
| Equals(Object) |
Détermine si l’objet spécifié est égal à l’objet actuel. (Hérité de Object) |
| GetEnumerator() |
Retourne un IDictionaryEnumerator objet qui itère dans la OrderedDictionary collection. |
| GetHashCode() |
Sert de fonction de hachage par défaut. (Hérité de Object) |
| GetObjectData(SerializationInfo, StreamingContext) |
Implémente l’interface ISerializable et retourne les données nécessaires pour sérialiser la OrderedDictionary collection. |
| GetType() |
Obtient la Type de l’instance actuelle. (Hérité de Object) |
| Insert(Int32, Object, Object) |
Insère une nouvelle entrée dans la OrderedDictionary collection avec la clé et la valeur spécifiées à l’index spécifié. |
| MemberwiseClone() |
Crée une copie superficielle du Objectactuel. (Hérité de Object) |
| OnDeserialization(Object) |
Implémente l’interface ISerializable et est rappelée par l’événement de désérialisation lorsque la désérialisation est terminée. |
| Remove(Object) |
Supprime l’entrée avec la clé spécifiée de la OrderedDictionary collection. |
| RemoveAt(Int32) |
Supprime l’entrée à l’index spécifié de la OrderedDictionary collection. |
| ToString() |
Retourne une chaîne qui représente l’objet actuel. (Hérité de Object) |
Implémentations d’interfaces explicites
| Nom | Description |
|---|---|
| ICollection.IsSynchronized |
Obtient une valeur indiquant si l’accès à l’objet OrderedDictionary est synchronisé (thread-safe). |
| ICollection.SyncRoot |
Obtient un objet qui peut être utilisé pour synchroniser l’accès à l’objet OrderedDictionary . |
| IDeserializationCallback.OnDeserialization(Object) |
Implémente l’interface ISerializable et est rappelée par l’événement de désérialisation lorsque la désérialisation est terminée. |
| IDictionary.IsFixedSize |
Obtient une valeur indiquant si la OrderedDictionary taille est fixe. |
| IEnumerable.GetEnumerator() |
Retourne un IDictionaryEnumerator objet qui itère dans la OrderedDictionary collection. |
Méthodes d’extension
| Nom | Description |
|---|---|
| AsParallel(IEnumerable) |
Active la parallélisation d’une requête. |
| AsQueryable(IEnumerable) |
Convertit un IEnumerable en IQueryable. |
| Cast<TResult>(IEnumerable) |
Convertit les éléments d’un IEnumerable en type spécifié. |
| OfType<TResult>(IEnumerable) |
Filtre les éléments d’une IEnumerable en fonction d’un type spécifié. |