ListDictionary Klasa

Definicja

Implementuje IDictionary przy użyciu połączonej listy singly. Zalecane w przypadku kolekcji, które zwykle zawierają mniej niż 10 elementów.

C#
public class ListDictionary : System.Collections.IDictionary
C#
[System.Serializable]
public class ListDictionary : System.Collections.IDictionary
Dziedziczenie
ListDictionary
Atrybuty
Implementuje

Przykłady

Poniższy przykład kodu przedstawia kilka właściwości i metod .ListDictionary

C#
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


*/

Uwagi

Jest to prosta implementacja IDictionary używania połączonej listy singly. Jest ona mniejsza i szybsza niż Hashtable wartość, jeśli liczba elementów wynosi 10 lub mniej. Nie należy tego używać, jeśli wydajność jest ważna dla dużej liczby elementów.

Elementy w obiekcie nie są w żadnej gwarantowanej ListDictionary kolejności; kod nie powinien zależeć od bieżącej kolejności. Element ListDictionary jest implementowany do szybkiego pobierania kluczy. Rzeczywista wewnętrzna kolejność elementów jest zależna od implementacji i może ulec zmianie w przyszłych wersjach produktu.

Elementy członkowskie, takie jak Item[], Add, Removei Contains , są operacjami O(n), gdzie n to Count.

Kluczem nie może być null, ale wartość może.

Instrukcja foreach języka C# (for each w Visual Basic) zwraca obiekt typu elementów w kolekcji. Ponieważ każdy element ListDictionary elementu jest parą klucz/wartość, typ elementu nie jest typem klucza ani typem wartości. Zamiast tego typ elementu to DictionaryEntry. Na przykład:

C#
foreach (DictionaryEntry de in myListDictionary)
{
    //...
}

Instrukcja foreach jest otoką wokół modułu wyliczającego, który umożliwia tylko odczyt z kolekcji, a nie do zapisu.

Konstruktory

ListDictionary()

Tworzy pustą ListDictionary wartość przy użyciu domyślnego porównania.

ListDictionary(IComparer)

Tworzy pustą ListDictionary wartość przy użyciu określonego porównania.

Właściwości

Count

Pobiera liczbę par klucz/wartość zawartych w obiekcie ListDictionary.

IsFixedSize

Pobiera wartość wskazującą, czy ma ListDictionary stały rozmiar.

IsReadOnly

Pobiera wartość wskazującą, czy kolekcja ListDictionary jest przeznaczona tylko do odczytu.

IsSynchronized

Pobiera wartość wskazującą, czy ListDictionary element jest zsynchronizowany (bezpieczny wątk).

Item[Object]

Pobiera lub ustawia wartość skojarzona z określonym kluczem.

Keys

Pobiera element ICollection zawierający klucze w obiekcie ListDictionary.

SyncRoot

Pobiera obiekt, który może służyć do synchronizowania dostępu do obiektu ListDictionary.

Values

Pobiera element ICollection zawierający wartości w obiekcie ListDictionary.

Metody

Add(Object, Object)

Dodaje wpis z określonym kluczem i wartością do elementu ListDictionary.

Clear()

Usuwa wszystkie wpisy z elementu ListDictionary.

Contains(Object)

Określa, czy element ListDictionary zawiera określony klucz.

CopyTo(Array, Int32)

Kopiuje ListDictionary wpisy do jednowymiarowego Array wystąpienia w określonym indeksie.

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetEnumerator()

Zwraca wartość IDictionaryEnumerator , która iteruje za pośrednictwem elementu ListDictionary.

GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
Remove(Object)

Usuwa wpis z określonym kluczem z .ListDictionary

ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Jawne implementacje interfejsu

IEnumerable.GetEnumerator()

Zwraca wartość IEnumerator , która iteruje za pośrednictwem elementu ListDictionary.

Metody rozszerzania

Cast<TResult>(IEnumerable)

Rzutuje elementy obiektu IEnumerable na określony typ.

OfType<TResult>(IEnumerable)

Filtruje elementy IEnumerable elementu na podstawie określonego typu.

AsParallel(IEnumerable)

Umożliwia równoległość zapytania.

AsQueryable(IEnumerable)

Konwertuje element IEnumerable na .IQueryable

Dotyczy

Produkt Wersje
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

Bezpieczeństwo wątkowe

Publiczne statyczne (Shared w Visual Basic) elementy członkowskie tego typu są bezpieczne wątkami. Wystąpienia elementów członkowskich nie dają gwarancji bezpieczeństwa wątków.

Ta implementacja nie zapewnia zsynchronizowanej (bezpiecznej wątku) otoki dla ListDictionaryklasy pochodnej , ale klasy pochodne mogą tworzyć własne zsynchronizowane wersje ListDictionary właściwości SyncRoot .

Wyliczanie w kolekcji nie jest wewnętrznie procedurą odporną na wielowątkowość. Nawet gdy kolekcja jest synchronizowana, inne wątki nadal mogą ją modyfikować. Powoduje to zgłaszanie wyjątku przez moduł wyliczający. Aby zagwarantować bezpieczeństwo wątków podczas wyliczania, można zablokować kolekcję podczas całego procesu wyliczania albo rejestrować wyjątki wynikłe ze zmian wprowadzanych przez inne wątków.

Zobacz też