Heim  >  Artikel  >  Backend-Entwicklung  >  Gemischte Wörterbuchklassen in C#?

Gemischte Wörterbuchklassen in C#?

王林
王林nach vorne
2023-09-08 11:57:09682Durchsuche

C# 中的混合字典类?

Die HybridDictionary-Klasse implementiert IDictionary, indem sie ListDictionary verwendet, wenn die Sammlung klein ist, und wechselt dann zu Hashtable, wenn die Sammlung größer wird.

Das Folgende sind die Eigenschaften der HybridDictionary-Klasse:

Seriennummer Eigenschaften und Beschreibung
1 Anzahl

Ermitteln Sie die Anzahl der enthaltenen Schlüssel/Wert-Paare

Gemischtes Wörterbuch.

2 IsFixedSize

Erhält einen Wert, der angibt, ob HybridDictionary hat eine feste Größe.

3 IsReadOnly

Ruft einen Wert ab, der angibt, ob das HybridDictionary vorhanden ist Nur lesen.

4 IsSynchronized

Ruft einen Wert ab, der angibt, ob das HybridDictionary vorhanden ist synchronisiert (threadsicher).

5 Item[Object]

Ruft den mit dem angegebenen Schlüssel verknüpften Wert ab oder legt diesen fest.

6 Keys

Holen Sie sich die ICollection mit den Schlüsseln HybridDictionary.

7 SyncRoot

Stellen Sie Objekte für den synchronen Zugriff bereit zu HybridDictionary.

8 Werte

Holen Sie sich eine ICollection von Werten, die in einem HybridDictionary enthalten sind das HybridDictionary.

Im Folgenden sind einige Methoden der HybridDictionary-Klasse aufgeführt:

Seriennummer Methoden und. Beschreibung
1 Hinzufügen(Objekt, Objekt)

Es werden Einträge mit angegebenen Schlüsseln und Werten hinzugefügt das HybridDictionary.

2 Clear()

Entfernt alle Einträge aus dem HybridDictionary.

3 Contains(Object)

Bestimmt, ob das HybridDictionary einen bestimmten Schlüssel enthält.

4 CopyTo(Array, Int32)

Kopieren Sie die Einträge von HybridDictionary in ein eindimensionales Array Die Array-Instanz am angegebenen Index.

5 Equals(Object)

Bestimmen Sie, ob das angegebene Objekt gleich ist aktuelles Objekt. (Von Object geerbt)

6 GetEnumerator()

Gibt einen IDictionaryEnumerator für die Durchquerung zurück HybridDictionary.

7 GetHashCode() strong>

wird als Standard-Hash-Funktion verwendet. (Von Object geerbt)

8 GetType()

Den Typ der aktuellen Instanz abrufen. (Von Object geerbt)

Um die Anzahl der Schlüssel-Wert-Paare in HybridDictionary zu zählen, lautet der Code wie folgt: −

Beispiel

Jetzt sehen wir uns einige Beispiele an −

Demonstration

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      HybridDictionary dict1 = new HybridDictionary();
      dict1.Add("A", "SUV");
      dict1.Add("B", "MUV");
      dict1.Add("C", "AUV");
      Console.WriteLine("HybridDictionary1 elements...");
      foreach(DictionaryEntry d in dict1) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Count of Key/value pairs in Dictionary1 = "+dict1.Count);
      HybridDictionary dict2 = new HybridDictionary();
      dict2.Add("1", "One");
      dict2.Add("2", "Two");
      dict2.Add("3", "Three");
      dict2.Add("4", "Four");
      dict2.Add("5", "Five");
      dict2.Add("6", "Six");
      Console.WriteLine("HybridDictionary2 elements...");
      foreach(DictionaryEntry d in dict2) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Count of Key/value pairs in Dictionary2 = "+dict1.Count);
      dict2.Clear();
      Console.WriteLine("Count of Key/value pairs in Dictionary2 (Updated) = "+dict2.Count);
   }
}

Ausgabe

Dies erzeugt die folgende Ausgabe:

HybridDictionary1 elements...
A SUV
B MUV
C AUV
Count of Key/value pairs in Dictionary1 = 3
HybridDictionary2 elements...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
Count of Key/value pairs in Dictionary2 = 3
Count of Key/value pairs in Dictionary2 (Updated) = 0

Um zu überprüfen, ob das HybridDictionary synchronisiert ist oder nicht, lautet der Code wie folgt:

Beispiel

Live-Demonstration

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      HybridDictionary dict1 = new HybridDictionary();
      dict1.Add("A", "Books");
      dict1.Add("B", "Electronics");
      dict1.Add("C", "Smart Wearables");
      dict1.Add("D", "Pet Supplies");
      dict1.Add("E", "Clothing");
      dict1.Add("F", "Footwear");
      Console.WriteLine("HybridDictionary1 elements...");
      foreach(DictionaryEntry d in dict1) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Is the HybridDictionary1 having fixed size? = "+dict1.IsFixedSize);
      Console.WriteLine("If HybridDictionary1 read-only? = "+dict1.IsReadOnly);
      Console.WriteLine("Is HybridDictionary1 synchronized = "+dict1.IsSynchronized);
      HybridDictionary dict2 = new HybridDictionary();
      dict2.Add("1", "One");
      dict2.Add("2", "Two");
      dict2.Add("3", "Three");
      dict2.Add("4", "Four");
      dict2.Add("5", "Five");
      dict2.Add("6", "Six");
      Console.WriteLine("HybridDictionary2 elements...");
      foreach(DictionaryEntry d in dict2) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("Is HybridDictionary1 equal to HybridDictionary2? = "+(dict1.Equals(dict2)));
      Console.WriteLine("Is the HybridDictionary2 having fixed size? = "+dict2.IsFixedSize);
      Console.WriteLine("If HybridDictionary2 read-only? = "+dict2.IsReadOnly);
      Console.WriteLine("Is HybridDictionary2 synchronized = "+dict2.IsSynchronized);
   }
}

Ausgabe

Dies erzeugt die folgende Ausgabe:

HybridDictionary1 elements...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Is the HybridDictionary1 having fixed size? = False
If HybridDictionary1 read-only? = False
Is HybridDictionary1 synchronized = False
HybridDictionary2 elements...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
Is HybridDictionary1 equal to HybridDictionary2? = False
Is the HybridDictionary2 having fixed size? = False
If HybridDictionary2 read-only? = False
Is HybridDictionary2 synchronized = False

Das obige ist der detaillierte Inhalt vonGemischte Wörterbuchklassen in C#?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:tutorialspoint.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen