ListDictionary Klas
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
IDictionary Implementeert met behulp van een singly linked list. Aanbevolen voor verzamelingen die doorgaans minder dan 10 items bevatten.
public ref class ListDictionary : System::Collections::IDictionary
public class ListDictionary : System.Collections.IDictionary
[System.Serializable]
public class ListDictionary : System.Collections.IDictionary
type ListDictionary = class
interface ICollection
interface IEnumerable
interface IDictionary
[<System.Serializable>]
type ListDictionary = class
interface IDictionary
interface ICollection
interface IEnumerable
Public Class ListDictionary
Implements IDictionary
- Overname
-
ListDictionary
- Kenmerken
- Implementeringen
Voorbeelden
In het volgende codevoorbeeld ziet u verschillende eigenschappen en methoden van ListDictionary.
using System;
using System.Collections;
using System.Collections.Specialized;
public class SamplesListDictionary {
public static void Main() {
// Creates and initializes a new ListDictionary.
ListDictionary myCol = new ListDictionary();
myCol.Add( "Braeburn Apples", "1.49" );
myCol.Add( "Fuji Apples", "1.29" );
myCol.Add( "Gala Apples", "1.49" );
myCol.Add( "Golden Delicious Apples", "1.29" );
myCol.Add( "Granny Smith Apples", "0.89" );
myCol.Add( "Red Delicious Apples", "0.99" );
// 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 IDictionaryEnumerator:" );
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 ListDictionary 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,-25} {1}", myArr[i].Key, myArr[i].Value );
Console.WriteLine();
// Searches for a key.
if ( myCol.Contains( "Kiwis" ) )
Console.WriteLine( "The collection contains the key \"Kiwis\"." );
else
Console.WriteLine( "The collection does not contain the key \"Kiwis\"." );
Console.WriteLine();
// Deletes a key.
myCol.Remove( "Plums" );
Console.WriteLine( "The collection contains the following elements after removing \"Plums\":" );
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( IDictionary 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( IDictionary myCol ) {
IDictionaryEnumerator myEnumerator = myCol.GetEnumerator();
Console.WriteLine( " KEY VALUE" );
while ( myEnumerator.MoveNext() )
Console.WriteLine( " {0,-25} {1}", myEnumerator.Key, myEnumerator.Value );
Console.WriteLine();
}
// Uses the Keys, Values, Count, and Item properties.
public static void PrintKeysAndValues3( ListDictionary 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 output similar to the following.
Note that because a dictionary is implemented for fast keyed access the order
of the items in the dictionary are not gauranteed and, as a result, should not
be depended on.
Displays the elements using foreach:
KEY VALUE
Braeburn Apples 1.49
Fuji Apples 1.29
Gala Apples 1.49
Golden Delicious Apples 1.29
Granny Smith Apples 0.89
Red Delicious Apples 0.99
Displays the elements using the IDictionaryEnumerator:
KEY VALUE
Braeburn Apples 1.49
Fuji Apples 1.29
Gala Apples 1.49
Golden Delicious Apples 1.29
Granny Smith Apples 0.89
Red Delicious Apples 0.99
Displays the elements using the Keys, Values, Count, and Item properties:
INDEX KEY VALUE
0 Braeburn Apples 1.49
1 Fuji Apples 1.29
2 Gala Apples 1.49
3 Golden Delicious Apples 1.29
4 Granny Smith Apples 0.89
5 Red Delicious Apples 0.99
Displays the elements in the array:
KEY VALUE
Braeburn Apples 1.49
Fuji Apples 1.29
Gala Apples 1.49
Golden Delicious Apples 1.29
Granny Smith Apples 0.89
Red Delicious Apples 0.99
The collection does not contain the key "Kiwis".
The collection contains the following elements after removing "Plums":
KEY VALUE
Braeburn Apples 1.49
Fuji Apples 1.29
Gala Apples 1.49
Golden Delicious Apples 1.29
Granny Smith Apples 0.89
Red Delicious Apples 0.99
The collection contains the following elements after it is cleared:
KEY VALUE
*/
Imports System.Collections
Imports System.Collections.Specialized
Public Class SamplesListDictionary
Public Shared Sub Main()
' Creates and initializes a new ListDictionary.
Dim myCol As New ListDictionary()
myCol.Add("Braeburn Apples", "1.49")
myCol.Add("Fuji Apples", "1.29")
myCol.Add("Gala Apples", "1.49")
myCol.Add("Golden Delicious Apples", "1.29")
myCol.Add("Granny Smith Apples", "0.89")
myCol.Add("Red Delicious Apples", "0.99")
' Display the contents of the collection using For Each. This is the preferred method.
Console.WriteLine("Displays the elements using For Each:")
PrintKeysAndValues(myCol)
' Display the contents of the collection using the enumerator.
Console.WriteLine("Displays the elements using the IDictionaryEnumerator:")
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 ListDictionary 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,-25} {1}", myArr(i).Key, myArr(i).Value)
Next i
Console.WriteLine()
' Searches for a key.
If myCol.Contains("Kiwis") Then
Console.WriteLine("The collection contains the key ""Kiwis"".")
Else
Console.WriteLine("The collection does not contain the key ""Kiwis"".")
End If
Console.WriteLine()
' Deletes a key.
myCol.Remove("Plums")
Console.WriteLine("The collection contains the following elements after removing ""Plums"":")
PrintKeysAndValues(myCol)
' Clears the entire collection.
myCol.Clear()
Console.WriteLine("The collection contains the following elements after it is cleared:")
PrintKeysAndValues(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 PrintKeysAndValues(myCol As IDictionary)
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 IDictionary)
Dim myEnumerator As IDictionaryEnumerator = myCol.GetEnumerator()
Console.WriteLine(" KEY VALUE")
While myEnumerator.MoveNext()
Console.WriteLine(" {0,-25} {1}", myEnumerator.Key, myEnumerator.Value)
End While
Console.WriteLine()
End Sub
' Uses the Keys, Values, Count, and Item properties.
Public Shared Sub PrintKeysAndValues3(myCol As ListDictionary)
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.
'Note that because a dictionary is implemented for fast keyed access the order
'of the items in the dictionary are not gauranteed and, as a result, should not
'be depended on.
'
'Displays the elements using for each:
' KEY VALUE
' Braeburn Apples 1.49
' Fuji Apples 1.29
' Gala Apples 1.49
' Golden Delicious Apples 1.29
' Granny Smith Apples 0.89
' Red Delicious Apples 0.99
'
'Displays the elements using the IDictionaryEnumerator:
' KEY VALUE
' Braeburn Apples 1.49
' Fuji Apples 1.29
' Gala Apples 1.49
' Golden Delicious Apples 1.29
' Granny Smith Apples 0.89
' Red Delicious Apples 0.99
'
'Displays the elements using the Keys, Values, Count, and Item properties:
' INDEX KEY VALUE
' 0 Braeburn Apples 1.49
' 1 Fuji Apples 1.29
' 2 Gala Apples 1.49
' 3 Golden Delicious Apples 1.29
' 4 Granny Smith Apples 0.89
' 5 Red Delicious Apples 0.99
'
'Displays the elements in the array:
' KEY VALUE
' Braeburn Apples 1.49
' Fuji Apples 1.29
' Gala Apples 1.49
' Golden Delicious Apples 1.29
' Granny Smith Apples 0.89
' Red Delicious Apples 0.99
'
'The collection does not contain the key "Kiwis".
'
'The collection contains the following elements after removing "Plums":
' KEY VALUE
' Braeburn Apples 1.49
' Fuji Apples 1.29
' Gala Apples 1.49
' Golden Delicious Apples 1.29
' Granny Smith Apples 0.89
' Red Delicious Apples 0.99
'
'The collection contains the following elements after it is cleared:
' KEY VALUE
'
Opmerkingen
Dit is een eenvoudige implementatie van het gebruik van IDictionary een singly linked list. Het is kleiner en sneller dan een Hashtable als het aantal elementen 10 of minder is. Dit mag niet worden gebruikt als de prestaties belangrijk zijn voor grote aantallen elementen.
Items in een ListDictionary zijn niet in een gegarandeerde volgorde; code mag niet afhankelijk zijn van de huidige volgorde. De ListDictionary is geïmplementeerd voor snel sleutel ophalen; de werkelijke interne volgorde van items is afhankelijk van de implementatie en kan veranderen in toekomstige versies van het product.
Leden, zoals Item[], Add, en RemoveContains zijn O(n) bewerkingen, waar n is Count.
Een sleutel kan niet zijn null, maar een waarde kan.
De instructie foreach van de C#-taal (for each in Visual Basic) retourneert een object van het type elementen in de verzameling. Omdat elk element van het ListDictionary element een sleutel/waardepaar is, is het elementtype niet het type van de sleutel of het type van de waarde. In plaats daarvan is DictionaryEntryhet elementtype . Voorbeeld:
foreach (DictionaryEntry de in myListDictionary)
{
//...
}
For Each de As DictionaryEntry In myListDictionary
'...
Next de
De foreach instructie is een wrapper rond de enumerator, waarmee alleen kan worden gelezen van, niet schrijven naar, de verzameling.
Constructors
| Name | Description |
|---|---|
| ListDictionary() |
Hiermee maakt u een lege ListDictionary met behulp van de standaard-vergelijkingsfunctie. |
| ListDictionary(IComparer) |
Hiermee maakt u een lege ListDictionary met behulp van de opgegeven vergelijkingsfunctie. |
Eigenschappen
| Name | Description |
|---|---|
| Count |
Hiermee haalt u het aantal sleutel-/waardeparen op dat is opgenomen in de ListDictionary. |
| IsFixedSize |
Hiermee wordt een waarde opgehaald die aangeeft of de grootte van een ListDictionary vaste grootte is. |
| IsReadOnly |
Hiermee wordt een waarde opgehaald die aangeeft of het ListDictionary kenmerk Alleen-lezen is. |
| IsSynchronized |
Hiermee wordt een waarde opgehaald die aangeeft of de ListDictionary synchronisatie is uitgevoerd (thread safe). |
| Item[Object] |
Hiermee haalt u de waarde op die is gekoppeld aan de opgegeven sleutel of stelt u deze in. |
| Keys |
Hiermee haalt u een ICollection met de sleutels op in de ListDictionary. |
| SyncRoot |
Hiermee haalt u een object op dat kan worden gebruikt om de toegang tot het ListDictionaryobject te synchroniseren. |
| Values |
Hiermee haalt u een ICollection met de waarden in de ListDictionary. |
Methoden
| Name | Description |
|---|---|
| Add(Object, Object) |
Voegt een vermelding toe met de opgegeven sleutel en waarde in de ListDictionary. |
| Clear() |
Hiermee verwijdert u alle vermeldingen uit de ListDictionary. |
| Contains(Object) |
Bepaalt of de ListDictionary sleutel een specifieke sleutel bevat. |
| CopyTo(Array, Int32) |
Kopieert de ListDictionary vermeldingen naar een eendimensionaal Array exemplaar op de opgegeven index. |
| Equals(Object) |
Bepaalt of het opgegeven object gelijk is aan het huidige object. (Overgenomen van Object) |
| GetEnumerator() |
Retourneert een IDictionaryEnumerator die door de ListDictionary. |
| GetHashCode() |
Fungeert als de standaardhashfunctie. (Overgenomen van Object) |
| GetType() |
Hiermee haalt u de Type huidige instantie op. (Overgenomen van Object) |
| MemberwiseClone() |
Hiermee maakt u een ondiepe kopie van de huidige Object. (Overgenomen van Object) |
| Remove(Object) |
Hiermee verwijdert u de vermelding met de opgegeven sleutel uit de ListDictionary. |
| ToString() |
Retourneert een tekenreeks die het huidige object vertegenwoordigt. (Overgenomen van Object) |
Expliciete interface-implementaties
| Name | Description |
|---|---|
| IEnumerable.GetEnumerator() |
Retourneert een IEnumerator die door de ListDictionary. |
Extensiemethoden
| Name | Description |
|---|---|
| AsParallel(IEnumerable) |
Hiermee schakelt u parallelle uitvoering van een query in. |
| AsQueryable(IEnumerable) |
Converteert een IEnumerable naar een IQueryable. |
| Cast<TResult>(IEnumerable) |
Cast de elementen van een IEnumerable naar het opgegeven type. |
| OfType<TResult>(IEnumerable) |
Hiermee filtert u de elementen van een IEnumerable op basis van een opgegeven type. |
Van toepassing op
Veiligheid thread
Openbare statische (Shared in Visual Basic) leden van dit type zijn thread-veilig. Exemplaarleden zijn niet gegarandeerd thread-safe.
Deze implementatie biedt geen gesynchroniseerde wrapper (thread safe) voor een ListDictionary, maar afgeleide klassen kunnen hun eigen gesynchroniseerde versies van de ListDictionarySyncRoot eigenschap maken.
Het inventariseren via een verzameling is intrinsiek geen thread-veilige procedure. Zelfs wanneer een verzameling wordt gesynchroniseerd, kunnen andere threads de verzameling nog steeds wijzigen, waardoor de enumerator een uitzondering genereert. Om de veiligheid van threads tijdens de inventarisatie te garanderen, kunt u de verzameling vergrendelen tijdens de volledige inventarisatie of de uitzonderingen ondervangen die het gevolg zijn van wijzigingen die door andere threads zijn aangebracht.