Assembly : mscorlib (dans mscorlib.dll)
Syntaxe<ComVisibleAttribute(True)> _ Public Interface IDictionary Inherits ICollection, IEnumerable
Dim instance As IDictionary
[ComVisibleAttribute(true)] public interface IDictionary : ICollection, IEnumerable
[ComVisibleAttribute(true)] public interface class IDictionary : ICollection, IEnumerable
/** @attribute ComVisibleAttribute(true) */ public interface IDictionary extends ICollection, IEnumerable
ComVisibleAttribute(true) public interface IDictionary extends ICollection, IEnumerable
NotesL'interface IDictionary est l'interface de base pour les collections non génériques de paires clé/valeur. Pour la version générique de cette interface, consultez System.Collections.Generic.IDictionary.
Chaque élément est une paire clé/valeur stockée dans un objet DictionaryEntry.
Chaque paire doit avoir une clé unique. Les implémentations peuvent varier selon que la clé a la valeur référence Null (Nothing en Visual Basic) ou non. La valeur peut être référence Null (Nothing en Visual Basic) et il n'est pas nécessaire qu'elle soit unique. L'interface IDictionary permet d'énumérer les clés et valeurs, mais n'impose pas un ordre de tri particulier.
Les implémentations de IDictionary peuvent être classées dans trois catégories : lecture seule, taille fixe ou taille variable. Un objet IDictionary en lecture seule ne peut pas être modifié. Un objet IDictionary de taille fixe ne permet pas l'ajout ou la suppression d'éléments, mais permet la modification des éléments existants. Un objet IDictionary de taille variable permet l'ajout, la suppression et la modification des éléments.
L'instruction foreach du langage C# (for each en Visual Basic) requiert le type de chaque élément de la collection. Étant donné que chaque élément de l'objet IDictionary est une paire clé/valeur, le type d'élément n'est pas le type de la clé, ni le type de la valeur. Le type d'élément est plutôt DictionaryEntry. Par exemple :
foreach (DictionaryEntry de in myHashtable) {...}
For Each de As DictionaryEntry In myHashtable ... Next myDE
L'instruction foreach est un wrapper autour de l'énumérateur, qui permet la lecture à partir de la collection, mais non l'écriture dans celle-ci.
Remarques à l'attention des implémenteurs La classe d'implémentation doit disposer d'un moyen de comparer des clés.
ExempleL'exemple de code suivant montre comment définir une classe de dictionnaire simple qui implémente l'interface IDictionary.
' This class implements a simple dictionary using an array of DictionaryEntry objects (key/value pairs). Public Class SimpleDictionary Implements IDictionary ' The array of items Dim items() As DictionaryEntry Dim ItemsInUse As Integer = 0 ' Construct the SimpleDictionary with the desired number of items. ' The number of items cannot change for the life time of this SimpleDictionary. Public Sub New(ByVal numItems As Integer) items = New DictionaryEntry(numItems - 1) {} End Sub ' IDictionary Members Public ReadOnly Property IsReadOnly() As Boolean Implements IDictionary.IsReadOnly Get Return False End Get End Property Public Function Contains(ByVal key As Object) As Boolean Implements IDictionary.Contains Dim index As Integer Return TryGetIndexOfKey(key, index) End Function Public ReadOnly Property IsFixedSize() As Boolean Implements IDictionary.IsFixedSize Get Return False End Get End Property Public Sub Remove(ByVal key As Object) Implements IDictionary.Remove If key = Nothing Then Throw New ArgumentNullException("key") End If ' Try to find the key in the DictionaryEntry array Dim index As Integer If TryGetIndexOfKey(key, index) Then ' If the key is found, slide all the items up. Array.Copy(items, index + 1, items, index, (ItemsInUse - index) - 1) ItemsInUse = ItemsInUse - 1 Else ' If the key is not in the dictionary, just return. End If End Sub Public Sub Clear() Implements IDictionary.Clear ItemsInUse = 0 End Sub Public Sub Add(ByVal key As Object, ByVal value As Object) Implements IDictionary.Add ' Add the new key/value pair even if this key already exists in the dictionary. If ItemsInUse = items.Length Then Throw New InvalidOperationException("The dictionary cannot hold any more items.") End If items(ItemsInUse) = New DictionaryEntry(key, value) ItemsInUse = ItemsInUse + 1 End Sub Public ReadOnly Property Keys() As ICollection Implements IDictionary.Keys Get ' Return an array where each item is a key. Dim keyArray() As Object = New Object(ItemsInUse - 1) {} Dim n As Integer For n = 0 To ItemsInUse - 1 keyArray(n) = items(n).Key Next n Return keyArray End Get End Property Public ReadOnly Property Values() As ICollection Implements IDictionary.Values Get ' Return an array where each item is a value. Dim valueArray() As Object = New Object(ItemsInUse - 1) {} Dim n As Integer For n = 0 To ItemsInUse - 1 valueArray(n) = items(n).Value Next n Return valueArray End Get End Property Public Property Item(ByVal key As Object) As Object Implements IDictionary.Item Get ' If this key is in the dictionary, return its value. Dim index As Integer If TryGetIndexOfKey(key, index) Then ' The key was found return its value. Return items(index).Value Else ' The key was not found return null. Return Nothing End If End Get Set(ByVal value As Object) ' If this key is in the dictionary, change its value. Dim index As Integer If TryGetIndexOfKey(key, index) Then ' The key was found change its value. items(index).Value = value Else ' This key is not in the dictionary add this key/value pair. Add(key, value) End If End Set End Property Private Function TryGetIndexOfKey(ByVal key As Object, ByRef index As Integer) As Boolean For index = 0 To ItemsInUse - 1 ' If the key is found, return true (the index is also returned). If items(index).Key.Equals(key) Then Return True End If Next index ' Key not found, return false (index should be ignored by the caller). Return False End Function
// This class implements a simple dictionary using an array of DictionaryEntry objects (key/value pairs). public class SimpleDictionary : IDictionary { // The array of items private DictionaryEntry[] items; private Int32 ItemsInUse = 0; // Construct the SimpleDictionary with the desired number of items. // The number of items cannot change for the life time of this SimpleDictionary. public SimpleDictionary(Int32 numItems) { items = new DictionaryEntry[numItems]; } #region IDictionary Members public bool IsReadOnly { get { return false; } } public bool Contains(object key) { Int32 index; return TryGetIndexOfKey(key, out index); } public bool IsFixedSize { get { return false; } } public void Remove(object key) { if (key == null) throw new ArgumentNullException("key"); // Try to find the key in the DictionaryEntry array Int32 index; if (TryGetIndexOfKey(key, out index)) { // If the key is found, slide all the items up. Array.Copy(items, index + 1, items, index, ItemsInUse - index - 1); ItemsInUse--; } else { // If the key is not in the dictionary, just return. } } public void Clear() { ItemsInUse = 0; } public void Add(object key, object value) { // Add the new key/value pair even if this key already exists in the dictionary. if (ItemsInUse == items.Length) throw new InvalidOperationException("The dictionary cannot hold any more items."); items[ItemsInUse++] = new DictionaryEntry(key, value); } public ICollection Keys { get { // Return an array where each item is a key. Object[] keys = new Object[ItemsInUse]; for (Int32 n = 0; n < ItemsInUse; n++) keys[n] = items[n].Key; return keys; } } public ICollection Values { get { // Return an array where each item is a value. Object[] values = new Object[ItemsInUse]; for (Int32 n = 0; n < ItemsInUse; n++) values[n] = items[n].Value; return values; } } public object this[object key] { get { // If this key is in the dictionary, return its value. Int32 index; if (TryGetIndexOfKey(key, out index)) { // The key was found; return its value. return items[index].Value; } else { // The key was not found; return null. return null; } } set { // If this key is in the dictionary, change its value. Int32 index; if (TryGetIndexOfKey(key, out index)) { // The key was found; change its value. items[index].Value = value; } else { // This key is not in the dictionary; add this key/value pair. Add(key, value); } } } private Boolean TryGetIndexOfKey(Object key, out Int32 index) { for (index = 0; index < ItemsInUse; index++) { // If the key is found, return true (the index is also returned). if (items[index].Key.Equals(key)) return true; } // Key not found, return false (index should be ignored by the caller). return false; } private class SimpleDictionaryEnumerator : IDictionaryEnumerator { // A copy of the SimpleDictionary object's key/value pairs. DictionaryEntry[] items; Int32 index = -1; public SimpleDictionaryEnumerator(SimpleDictionary sd) { // Make a copy of the dictionary entries currently in the SimpleDictionary object. items = new DictionaryEntry[sd.Count]; Array.Copy(sd.items, 0, items, 0, sd.Count); } // Return the current item. public Object Current { get { ValidateIndex(); return items[index]; } } // Return the current dictionary entry. public DictionaryEntry Entry { get { return (DictionaryEntry) Current; } } // Return the key of the current item. public Object Key { get { ValidateIndex(); return items[index].Key; } } // Return the value of the current item. public Object Value { get { ValidateIndex(); return items[index].Value; } } // Advance to the next item. public Boolean MoveNext() { if (index < items.Length - 1) { index++; return true; } return false; } // Validate the enumeration index and throw an exception if the index is out of range. private void ValidateIndex() { if (index < 0 || index >= items.Length) throw new InvalidOperationException("Enumerator is before or after the collection."); } // Reset the index to restart the enumeration. public void Reset() { index = -1; } } public IDictionaryEnumerator GetEnumerator() { // Construct and return an enumerator. return new SimpleDictionaryEnumerator(this); } #endregion #region ICollection Members public bool IsSynchronized { get { return false; } } public object SyncRoot { get { throw new NotImplementedException(); } } public int Count { get { return ItemsInUse; } } public void CopyTo(Array array, int index) { throw new NotImplementedException(); } #endregion #region IEnumerable Members IEnumerator IEnumerable.GetEnumerator() { // Construct and return an enumerator. return ((IDictionary)this).GetEnumerator(); } #endregion }
// This class implements a simple dictionary using an array of // DictionaryEntry objects (key/value pairs). public ref class SimpleDictionary : public IDictionary { // The array of items private: array<DictionaryEntry^>^ items; private: int itemsInUse; // Construct the SimpleDictionary with the desired number of // items. The number of items cannot change for the life time of // this SimpleDictionary. public: SimpleDictionary(int size) { items = gcnew array<DictionaryEntry^>(size); } #pragma region IDictionary Members public: property virtual bool IsReadOnly { bool get() { return false; } } public: virtual bool Contains(Object^ key) { int index; return TryGetIndexOfKey(key, &index); } public: virtual property bool IsFixedSize { bool get() { return false; } } public: virtual void Remove(Object^ key) { if (key == nullptr) { throw gcnew ArgumentNullException("key"); } // Try to find the key in the DictionaryEntry array int index; if (TryGetIndexOfKey(key, &index)) { // If the key is found, slide all the items down. Array::Copy(items, index + 1, items, index, itemsInUse - index - 1); itemsInUse--; } else { // If the key is not in the dictionary, just return. return; } } public: virtual void Clear() { itemsInUse = 0; } public: virtual void Add(Object^ key, Object^ value) { // Add the new key/value pair even if this key already exists // in the dictionary. if (itemsInUse == items->Length) { throw gcnew InvalidOperationException ("The dictionary cannot hold any more items."); } items[itemsInUse++] = gcnew DictionaryEntry(key, value); } public: virtual property ICollection^ Keys { ICollection^ get() { // Return an array where each item is a key. array<Object^>^ keys = gcnew array<Object^>(itemsInUse); for (int i = 0; i < itemsInUse; i++) { keys[i] = items[i]->Key; } return keys; } } public: virtual property ICollection^ Values { ICollection^ get() { // Return an array where each item is a value. array<Object^>^ values = gcnew array<Object^>(itemsInUse); for (int i = 0; i < itemsInUse; i++) { values[i] = items[i]->Value; } return values; } } public: virtual property Object^ default[Object^] { Object^ get(Object^ key) { // If this key is in the dictionary, return its value. int index; if (TryGetIndexOfKey(key, &index)) { // The key was found; return its value. return items[index]->Value; } else { // The key was not found; return null. return nullptr; } } void set(Object^ key, Object^ value) { // If this key is in the dictionary, change its value. int index; if (TryGetIndexOfKey(key, &index)) { // The key was found; change its value. items[index]->Value = value; } else { // This key is not in the dictionary; add this // key/value pair. Add(key, value); } } } private: bool TryGetIndexOfKey(Object^ key, int* index) { for (*index = 0; *index < itemsInUse; *index++) { // If the key is found, return true (the index is also // returned). if (items[*index]->Key->Equals(key)) { return true; } } // Key not found, return false (index should be ignored by // the caller). return false; }
Plates-formesWindows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile pour Pocket PC, Windows Mobile pour Smartphone, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition
Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.
Informations de version
Outils (masquer)
S'enregistrer
Liste des Membres
Qui est en ligne?
FAQ